无法使文件java.io.IOException:没有这样的文件或目录 [英] Cannot make file java.io.IOException: No such file or directory

查看:996
本文介绍了无法使文件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.

这篇关于无法使文件java.io.IOException:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆