【问题标题】:java.io.FileNotFoundException: .. (No such file or directory)java.io.FileNotFoundException: .. (没有这样的文件或目录)
【发布时间】:2015-05-30 18:55:01
【问题描述】:

我已经开发了一个在 Windows 操作系统下在 Eclipse 上正确运行的 Java 项目。然后我想切换到 Ubuntu。我刚刚从 Windows 中获取了完整的工作区文件夹(包括运行所需的所有资源文件)并将其导入到 Ubuntu 的 Eclipse 中。

最令人惊讶的是,我得到了以下异常:

[ Test ] ERROR: unable to create myClass object. 
Check your implementation requirements!
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at Test.main(Test.java:183)
Caused by: java.io.FileNotFoundException: raw\file1.txt (No such file or directory)
...

但是这样的目录和这样的文件确实存在于当前路径中。

为什么会发生这种情况?

【问题讨论】:

  • 虽然 Windows 可以接受反斜杠作为路径分隔符,但 Unix 不接受。使用正斜杠。

标签: java linux windows filenotfoundexception


【解决方案1】:

在任何项目中使用File.separator 代替"/"

【讨论】:

  • 我会说“在任何项目中”。
  • @DmitryGinzburg 确实如此,应该这么说。
【解决方案2】:

使用正斜杠“/”,因为它适用于 Windows 和非 Windows 平台的文件路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多