我的文件系统上有以下现有目录结构(使用 ext4 的 Ubuntu 12.04 桌面):
/
home/
myuser/
.myapp/
logs/
myapp.log
data/
lib/
...以及以下 Java 代码:
try {
// If no such file exists, create it and write zero (0) to it.
if(!myFile.exists()) {
System.out.println("myFile is: " + myFile.getAbsolutePath());
myFile.createNewFile();
myFileWriter.write("0"); // Configured to write to myFile
}
} catch(IOException ioExc) {
logger.error(ExceptionUtils.getStackTrace(ioExc));
throw new RuntimeException(ioExc);
}
...这是抛出以下异常:
myFile is: /home/myuser/.myapp/data/replay/Loader-0-replay.log
java.lang.RuntimeException: java.io.IOException: No such file or directory
at net.myuser.myapp.tools.myapp.Loader.<init>(Loader.java:69)
at net.myuser.myapp.tools.myapp.MyAppTool.loadWords(MyAppTool.java:125)
at net.myuser.myapp.tools.myapp.MyAppTool.run(MyAppTool.java:65)
at net.myuser.myapp.tools.myapp.MyAppTool.main(MyAppTool.java:41)
Caused by: java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:900)
at net.myuser.myapp.tools.myapp.Loader.<init>(Loader.java:62)
... 3 more
这里发生了什么?它说“没有这样的文件或目录”,但这不是createNewFile()
应该为我做的吗?提前致谢!