无法使文件java.io.IOException:没有这样的文件或目录

无法使文件java.io.IOException:没有这样的文件或目录

问题描述:

我正在尝试在文件系统上创建一个文件,但我一直遇到这个异常:

I am trying to create a file on the filesystem, but I keep getting this exception:

java.io.IOException: No such file or directory

我有一个现有目录,我正在尝试写一个文件到那个目录。

I have an existing directory, and I am trying to write a file to that directory.

// I have also tried this below, but get same error
// new File(System.getProperty("user.home") + "/.foo/bar/" + fileName);

File f = new File(System.getProperty("user.home") + "/.foo/bar/", fileName);

if (f.exists() && !f.canWrite())
        throw new IOException("Kan ikke skrive til filsystemet " + f.getAbsolutePath());

if (!f.isFile()) {
    f.createNewFile(); // Exception here
} else {
    f.setLastModified(System.currentTimeMillis());
}

获得例外:

java.io.IOException: No such file or directory
  at java.io.UnixFileSystem.createFileExclusively(Native Method)
  at java.io.File.createNewFile(File.java:883)`

我对路径有写权限,但文件不是'创建。

I have write permission to the path, however the file isn't created.

打印完整文件名或在调试器中单步执行。当我对这样的错误感到困惑时,这意味着我的假设和期望与现实不符。确保您可以查看路径是什么;它会帮助你弄清楚你哪里出错了。

Print the full file name out or step through in a debugger. When I get confused by errors like this, it means that my assumptions and expectations don't match reality. Make sure you can see what the path is; it'll help you figure out where you've gone wrong.