我有一個關於日食使用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.
看看這個帖子:http://stackoverflow.com/questions/15891510/java-io-filenotfoundexception-error-is-occuring – MarCrazyness