我在編碼java方面很新,而且我遇到了很多困難。 我想編寫一個程序使用bufferedreader從文件中讀取,我已經創建了名爲「scores.txt」。 所以我有一個名爲processFile的方法,它假設設置BufferedReader並循環遍歷文件,讀取每個分數。然後,我需要將分數轉換爲整數,將它們相加,並顯示計算出的平均值。java FileInputStream找不到文件
我不知道如何添加它們並計算平均值,但我目前正在從文件中讀取數據。 它一直說它不能罰款的文件,但我知道我的文件中有一個名爲「scores.txt」的文件。
這是我到目前爲止......這是非常糟糕的。我只是沒那麼這個:(好也許有一個不同的問題?
public static void main(String[] args) throws IOException,
FileNotFoundException {
String file = "scores.txt";
processFile("scores.txt");
//calls method processFile
}
public static void processFile (String file)
throws IOException, FileNotFoundException{
String line;
//lines is declared as a string
BufferedReader inputReader =
new BufferedReader (new InputStreamReader
(new FileInputStream(file)));
while ((line = inputReader.readLine()) != null){
System.out.println(line);
}
inputReader.close();
}
使用'Files.newBufferedReader()'入手 - 也是一個嘗試,與資源聲明 – fge
你缺少的文件路徑。現在你只有文件的名字。 –