java.io.FileNotFoundException: XXX (系統找不到指定的路徑。)


 1 import java.io.*;
 2 import java.util.Objects;
 3 
 4 public class CSVFile {
 5 
 6     public static void main(String[] args) {
 7         String filePath = Objects.requireNonNull(CSVFile.class.getClassLoader().getResource("data.txt")).getPath();
 8         printCSVFile(filePath);
 9     }
10     
11     public static void printCSVFile(String filePath) {
12         //add code here
13         System.out.println("Last    Fisrt    Salary");
14         try {
15             BufferedReader br = new BufferedReader(new FileReader(filePath));
16             String temp;
17             while((temp = br.readLine()) != null) {
18                 String[] tempList = temp.split(",");
19                 for(int i = 0; i < tempList.length; i++) {
20                     if(i < tempList.length - 1) {
21                         System.out.print(tempList[i] + "    ");
22                     }
23                     else {
24                         System.out.println(tempList[i]);
25                     }
26                 }
27             }
28         } catch (IOException ex) {
29             ex.printStackTrace();
30         }
31     }
32 
33 }

以上代碼檢查后發現沒有什么問題,但運行時報錯

 1 Last    Fisrt    Salary
 2 java.io.FileNotFoundException: D:\%e4%bd%9c%e4%b8%9a\%e8%bd%af%e4%bb%b6%e5%b7%a5%e7%a8%8b%e4%b8%8e%e8%ae%a1%e7%ae%97I\CodeSpace\2e6400dff19b495493514ed0a5c2cd25\236-testcsvfile\target\classes\data.txt (系統找不到指定的路徑。)
 3     at java.io.FileInputStream.open0(Native Method)
 4     at java.io.FileInputStream.open(FileInputStream.java:195)
 5     at java.io.FileInputStream.<init>(FileInputStream.java:138)
 6     at java.io.FileInputStream.<init>(FileInputStream.java:93)
 7     at java.io.FileReader.<init>(FileReader.java:58)
 8     at CSVFile.printCSVFile(CSVFile.java:15)
 9     at CSVFile.main(CSVFile.java:8)
10 
11 Process finished with exit code 0

百思不得其解,最終發現是因為文件路徑包含中文(lll¬ω¬)

重構系統文件目錄,改用英文刻不容緩!!!ㄟ( ▔, ▔ )ㄏ


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM