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¬ω¬)
重构系统文件目录,改用英文刻不容缓!!!ㄟ( ▔, ▔ )ㄏ
原文地址:https://www.cnblogs.com/Neptunejiang/p/12656321.html
时间: 2024-10-03 23:00:36