我在编码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
你缺少的文件路径。现在你只有文件的名字。 –