/*
*This program checks type casting from String to int/double from a file
*/
import java.io.*;
import java.lang.String;
public class ConvertingStringsToNums {
public static void main (String[] args){
File dataFile = new File("/files/scores.dat");
FileReader in;
BufferedReader readFile;
String score;
double avgScore, totalScores = 0;
int numScores = 0;
//------------------------------------------------------------
try {
in = new FileReader(dataFile);
readFile = new BufferedReader(in);
while((score = readFile.readLine()) != null) {
numScores += 1;
System.out.println(score);
totalScores += Double.parseDouble(score);
}
avgScore = totalScores/numScores;
readFile.close();
in.close();
} catch(FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
} //end try/catch
}
}
0
A
回答
0
0
的回答這個問題存在於javadocs for the File class:
For UNIX platforms, the prefix of an absolute pathname is always "/". Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix "/" and an empty name sequence.
在代碼中,你有以下幾種:
File dataFile = new File("/files/scores.dat");
根據文檔,這是一個絕對路徑,這意味着Java正在文件系統的根目錄中尋找名爲「files」的文件夾,然後在該文件夾中查找scores.dat。
如果你不是希望尋找一個文件目錄,是相對於當前目錄下,你需要省略第一/
:
File dataFile = new File("files/scores.dat");
另一種選擇是使用絕對路徑的數據文件,但如果更改項目的位置或將類文件放在JAR文件中,則可能會遇到問題。
嘗試打開你的日誌記錄級別爲DEBUG或ALL,這樣就可以清楚地看到該程序正在試圖尋找。這將幫助您調整您的代碼以定位正確的文件夾。
1
1)如果你想打開一個文件的絕對路徑硬盤驅動器上:
br = new BufferedReader (
new FileReader(
new File ("/files/scores.dat")));
2)如果您希望相對相對於在那裏你開始你的應用的路徑上打開一個文件:
br = new BufferedReader (
new FileReader(
new File ("files/scores.dat")));
3)如果您希望相對相對於你的類文件的路徑(包特別相關和/或從一個.jar或一個.war)在執行打開文件:
this.getClass().getResourceAsStream ("files/scores.dat");
「希望幫助
相關問題
- 1. 當文件存在時拋出FileNotFoundException
- 2. 文件存在於資產中但拋出FileNotFoundException
- 3. fileNotFoundException但文件存在
- 4. 刪除存在的文檔拋出FileNotFoundException?
- 5. 拋出FileNotFoundException但未捕獲
- 6. 爲什麼我的程序在文件存在時捕獲/拋出FileNotFoundException?
- 7. PrintWriter拋出FileNotFoundException
- 8. fileinputstream拋出filenotfoundexception?
- 9. 讀取文件時拋出FileNotFoundException異常
- 10. 引發FileNotFoundException但文件存在
- 11. Marshmallow拋出FileNotFoundException,但舊版本不會
- 12. Volley DiskBasedCache拋出FileNotFoundException
- 13. FileNotFoundException不被拋出
- 14. ClassPathResource.getFile()在WAR中拋出FileNotFoundException
- 15. FileNotFoundException文件存在於與程序相同的文件夾中
- 16. 存在Java FileNotFoundException文件(Android)
- 17. FileNotFoundException當文件存在時
- 18. FileNotFoundException即使文件存在
- 19. FileNotFoundException雖然文件存在
- 20. IntelliJ「FileNotFoundException」,文件存在
- 21. FileNotFoundException雖然文件存在
- 22. Java新文件()說FileNotFoundException但文件存在
- 23. CompileAssemblyFromDom拋出FileNotFoundException異常
- 24. .Assembly/GetExportedTypes拋出FileNotFoundException異常
- 25. 位圖工廠拋出FileNotFoundException
- 26. Android:FileReader意外拋出FileNotFoundException
- 27. Spark worker在臨時洗牌文件上拋出FileNotFoundException
- 28. ETL加載器拋出FileNotFoundException:加載json文件時出錯
- 29. 如果要添加的文件不存在,addFilePattern不會拋出FileNotFoundException
- 30. 使用Interop程序集時(即使文件存在),FileNotFoundException
如果你得到一個‘文件不存在’異常試圖打開‘/files/scores.dat’,那麼它是一個安全的賭注,因爲某些原因,「/文件/分.dat「不存在:)。建議:可以在不同的驅動器(C:vs D :)嗎?你是否從.jar執行程序(它會有一個不同的根目錄)?你能想到其他的解釋嗎? – paulsm4
不,文件夾「files」與.class文件位於同一文件中,而scores.dat文件位於該文件夾內。 – Raziel