2013-09-24 19 views
0

對於這個愚蠢的問題感到抱歉。但我真的無法找到我在做錯的地方。請幫忙。我正在嘗試使用JSON解析文件。文件也在系統中。但它顯示filenotfound異常。其真令人沮喪。文件未找到來自FileReader的異常

片斷的我的代碼如下:

 System.out.println("Please provide JSON file path : "); 
     filePathJson = "\"D:\\files\\test.xlsx\""; 
       //in.nextLine(); 

     System.out.println("Please provide Excel file path : "); 
     filePathExcel = in.nextLine(); 

     Object obj = parser.parse(new FileReader(filePathJson)); 
     System.out.println("hii"); 

     JSONArray array = new JSONArray(); 

和錯誤我得到:

Please provide JSON file path : 
Please provide Excel file path : 
"D:\\files\\test1.xlsx" 
java.io.FileNotFoundException: "D:\files\test.xlsx" (The filename, directory name, or volume label syntax is incorrect) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(FileInputStream.java:137) 
    at java.io.FileInputStream.<init>(FileInputStream.java:96) 
    at java.io.FileReader.<init>(FileReader.java:58) 
    at JavaJsonSplitter.main(JavaJsonSplitter.java:50) 

有人能指出我哪裏做錯了。

請忽略一個無用的sysout。

回答

4

你實際上把引號放在文件名中。刪除它們,你只需要在命令行中附帶實際的引號等等。當你給FileReader(或任何其他需要文件名,而不是命令行的方法)的文件名時,你只要給文件名(即使它有空格)。

例如,

filePathJson = "\"D:\\files\\test.xlsx\""; 

成爲

filePathJson = "D:\\files\\test.xlsx"; 
+0

誰幹的,改變這兩個文件的硬編碼和給定爲u建議..仍然面臨着同樣的問題... – user2696466

+0

@ user2696466:您可能會面臨另一個問題與文件名,但不是相同的問題。 –

2

刪除包圍文件路徑額外的雙引號。這根本不是必需的。

filePathJson = "D:\\files\\test.xlsx"; 
2
filePathJson = "\"D:\\files\\test.xlsx\""; 

應該像

filePathJson = "D:\\files\\test.xlsx"; 
0

@ user2696466 -

請參閱修改代碼,看看它是否工作

System.out.println("Please provide JSON file path : "); 
    **filePathJson = "D:/files/test.xlsx/"** 
      //in.nextLine(); 

    System.out.println("Please provide Excel file path : "); 
    filePathExcel = in.nextLine(); 

    Object obj = parser.parse(new FileReader(filePathJson)); 
    System.out.println("hii"); 

    JSONArray array = new JSONArray(); 
+0

對於一個有用的答案,總是說**你已經改變了什麼**和**爲什麼**。 (並且不要爲重點引入語法錯誤。) –

0

線索在這裏

java.io.FileNotFoundException: "D:\files\test.xlsx" (The filename, directory name, or volume label syntax is incorrect) 

試試這個

filePathJson = "D:\\files\\test.xlsx";