2016-02-28 70 views
0

我正嘗試在這種情況下使用intelij中的文件txt文件。我發現,如果我分辨出整個文件路徑如使用intelij中的文件IDEA

"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) 

該文件是存在的,我希望有人會能夠擺脫一些光線謝謝你。

+3

我建議你檢查你的工作目錄運行中的配置。我建議你打印出'System.out.println(System.getProperty(「user.dir」))' –

+0

或類似的'System.out.println(new File(「」)。getAbsolutePath())' –

+0

您。你在Windows下幫了我很多=) –

回答

4

當您嘗試

"files/newfile.txt" 

這是你是否使用/\\不應該在Windows下對重要

System.getProperty("user.dir") + File.pathSeparatorChar + "files/newfile.txt"; 

手短,但你必須確保當前的工作直接是你期望的。它可以在您的Run Configuration中設置。

要檢查這一點,你可以添加

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

「應該不重要」。 –

+0

謝謝你的輸入我改變了我的文件結構,所以沒有太多的項目正在進行,現在它的工作=)非常感謝你,你幫了我很多 –

相關問題