Java 在intelij IDEA中使用文件

Java 在intelij IDEA中使用文件,java,intellij-idea,Java,Intellij Idea,我正在尝试使用intelij中的文件,在本例中是txt文件。我发现如果我区分整个文件路径,例如 "C:\\Users\\benji\\java\\Project\\files\\newfile.txt" 工作,但如果我尝试 "files/newfile.txt" 我得到这个例外 Exception in thread "main" java.nio.file.NoSuchFileException: files\newfile.txt at sun.nio.fs.WindowsExcepti

我正在尝试使用intelij中的文件,在本例中是txt文件。我发现如果我区分整个文件路径,例如

"C:\\Users\\benji\\java\\Project\\files\\newfile.txt"
工作,但如果我尝试

"files/newfile.txt"
我得到这个例外

Exception in thread "main" java.nio.file.NoSuchFileException: files\newfile.txt
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:99)
at sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:278)
at java.nio.file.Files.copy(Files.java:1274)
at Main.main(Main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
文件就在那里,我希望有人能帮我解释一下,谢谢。

当你尝试时

"files/newfile.txt"
这是英语的缩写

System.getProperty("user.dir") + File.pathSeparatorChar + "files/newfile.txt";
在windows下,您是否使用了
/
\\
应该无关紧要,但您必须确保当前的直接工作是您所期望的。它可以在
运行配置中设置

要检查这一点,您可以添加

System.out.println("user.dir= " + System.getProperty("user.dir"));
当你尝试

"files/newfile.txt"
这是英语的缩写

System.getProperty("user.dir") + File.pathSeparatorChar + "files/newfile.txt";
在windows下,您是否使用了
/
\\
应该无关紧要,但您必须确保当前的直接工作是您所期望的。它可以在
运行配置中设置

要检查这一点,您可以添加

System.out.println("user.dir= " + System.getProperty("user.dir"));

我建议您在运行配置中检查工作目录。我建议您打印出
System.out.println(System.getProperty(“user.dir”))
或类似的
System.out.println(新文件(“”.getAbsolutePath())
谢谢。你帮了我很多=)我建议你在跑步配置中检查你的工作目录。我建议您打印出
System.out.println(System.getProperty(“user.dir”))
或类似的
System.out.println(新文件(“”.getAbsolutePath())
谢谢。你帮了我很多=)“不要紧”在Windows下。谢谢你的输入我更改了文件结构,所以没有很多项目在进行,现在可以正常工作=)非常感谢你帮了我“不要紧”在Windows下。谢谢你的输入我更改了文件结构,所以没有很多项目正在进行,现在可以正常工作了=)非常感谢你帮了我的忙