【问题标题】:Path Doesn't Exist路径不存在
【发布时间】:2014-06-13 13:12:44
【问题描述】:

我决定测试 Java 提供的文件创建可能性:

我试图制作一个在系统桌面上创建文件的程序。

为了获取位置,我执行了以下操作:

String targetLoc = System.getProperties("user.home") + "/Desktop"; //Returns /Users/targetUser/Desktop

然后创建文件:

File file = new File(targetLoc + "/testfile.txt"); //I'm aware of the slash before the name :)

try{

file.createNewFile();

}catch(Exception exception){

exception.printStackTrace();

}

即使我没有在上面看到任何错误,我也会收到 InvalidPathException

为什么会出现这个错误?

StackTrace 请求:

java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at com.Code0.FileCreater.Main.MainFF.main(MainFF.java:41)

答案:

这是一个简单的错误,我给主字符串变量分配了错误的值。

【问题讨论】:

  • 您有权在该位置创建该文件?
  • 你怎么能调用 System.getProperties("user.home")?
  • 我只在我的电脑上运行它,如果你的意思是@thumbmunkeys
  • 你能发布完整的堆栈跟踪吗?
  • 这真的是全部代码吗?在 try-catch 之外,您只有四行代码 - 1) 获取属性,2) 实例化 File 实例,3) 调用 createNewFile,以及 4) 打印堆栈跟踪。这些方法都不会抛出InvalidPathException(根据javadoc)。此外,System.getProperties 不接受参数。

标签: java file exception path


【解决方案1】:

这是一个简单的错误,我给主字符串变量分配了错误的值。

非常感谢大家的支持。

问候,

代码0

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多