2013-11-24 98 views
0

我有一個關於日食使用Java語言編寫的文件錯誤:java.io.FileNotFoundException:無效的文件路徑

public static void Write_Result(String Amount_Time_Dalta) throws IOException{ 

    File file; 
    FileOutputStream fop = null; 
    String content = ""; 
    String All_Result[] = Amount_Time_Dalta.split("-"); 
    String path ="/Users/Myname/Documents/workspace/ProjectHelper/"+All_Result[1] + ".csv"; 
    System.out.println(path); 
    content = All_Result[3]+ "," + All_Result[5] + "\n"; 
    System.out.println(content); 
    file = new File(path); 
    fop = new FileOutputStream(file); 
    file.getParentFile(); 

    if (!file.exists()) { 
     file.createNewFile(); 
    } 


    byte[] contentInBytes = content.getBytes(); 

    fop.write(contentInBytes); 
    fop.flush(); 
    fop.close(); 
} 

在當前項目編寫csv文件的問題,我得到的錯誤是

Exception in thread "main" java.io.FileNotFoundException: Invalid file path 
at java.io.FileOutputStream.<init>(FileOutputStream.java:215) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:171) 
at FileDistributor.Write_Result(FileDistributor.java:59) 
at FileDistributor.main(FileDistributor.java:29) 

我用

String path ="/Users/Myname/Documents/workspace/ProjectHelper/"; 

通道來讀取文件。我工作正常。但是,當我使用相同的路徑將結果寫入文件(可以存在與否,我創建或覆蓋文件)時,它會返回無效的文件路徑....我不太確定爲什麼......

更新

just found interesting thing. when i just use File newTextFile = new File("1000".csv); then it is working. however, when i replace to File newTextFile = new File(filename +".csv"); it doesn't work.

+0

看看這個帖子:http://stackoverflow.com/questions/15891510/java-io-filenotfoundexception-error-is-occuring – MarCrazyness

回答

0

你在這裏什麼是從一個File對象可以創建一個有效的路徑:

/Users/Myname/Documents/workspace/ProjectHelper/ 

但是,如果你看它第二次,你會看到,它是指一個目錄,而不是一個可寫文件。你的文件名是什麼?

你的System.out.println說的是All_Result[1]的值?

示例代碼:

import java.io.IOException; 
import java.io.File; 
import java.io.FileOutputStream; 

public class Test 
{ 
    public static void main(String[] args) 
    { 
     String[] array = {"1000.csv", "800.csv", "700.csv"}; 
     File file; 
     FileOutputStream fop; 

     // Uncomment these two lines 

     //String path = "c:\\" + array[0]; 
     //file = new File(path); 

     // And comment these next two lines, and the code still works 

     String path = "c:\\"; 
     file = new File (path + array[0]); 

     // Sanity check 

     System.out.println(path); 

     try 
     { 
      fop = new FileOutputStream(file); 
     } 
     catch(IOException e) 
     { 
      System.out.println("IOException opening output stream"); 
      e.printStackTrace(); 
     } 

     if (!file.exists()) 
     { 
      try 
      { 
       file.createNewFile(); 
      } 
      catch(IOException e) 
      { 
       System.out.println("IOException opening creating new file"); 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

爲了得到這個代碼打破,強似array[0]作爲文件名,只是傳遞一個空字符串""並且可以重現你的錯誤。

+0

MarsAtomic // it是字符串類型的數組。這將是一個文件的名稱。它會像1000.csv文件 –

+0

當然,但我不問「文件名應該是什麼?」我在問你文件名是什麼。當你將All_Result [1]追加到路徑中時,你可能會認爲你得到了「path/1000.csv」,但你實際上得到了「path /」,因爲All_Result [1]是空的。你甚至有一個System.out.println來仔細檢查,那麼那裏的實際輸出是什麼? – MarsAtomic

+0

它將是1000.csv .... 800.csv。我剛剛發現有趣的事情。當我只使用文件newTextFile =新文件(「1000」.csv);那麼它正在工作。但是,當我替換到文件newTextFile =新文件(文件名+「.csv」);它不起作用。 –

0

我遇到了同樣的問題,正在尋找答案。我嘗試使用string.trim()並將其放入輸出流,它工作。我猜測有一些尾隨的字符或文件路徑周圍的位