【发布时间】:2012-12-03 19:23:29
【问题描述】:
可能重复:
Where will be create a new file in java, when path is not given?
我正在尝试在当前目录中创建一个 txt 文件。我运行了下面的代码,没有错误。但是我在任何地方都找不到 myfile.txt 。我想在当前目录中创建 myfile.txt。我该怎么做?
我使用的代码:
public void createFile(){
try{
File f = new File("myfile.txt");
if (!f.exists()){
f.createNewFile();
System.out.println("New file \"myfile.txt\" has been created");
}
}catch(Exception e){
System.out.println("Error while creating file " + e);
}
}
【问题讨论】:
-
这就是它的作用。我建议你打印你的 cwd。
System.getProperty("user.dir") -
为什么不直接写下你想要存储的整个路径呢?
-
所有这一切都会创建一个空文件。如果你想在文件中放一些东西,你不需要这样做。
标签: java createfile