Java现在有如下格式的成绩单(文本格式)score.txt,内容如下∶ 姓名,数学,物理,英语 张三,72,67,70 李四,92,98,88 王五,68,80,77 要求按行读入取成绩单,并在该行的后面尾加上该同学的总成绩,然后再将该行写入到一个名字为 socreAnalysis.txt中
时间: 2023-09-27 14:06:58 浏览: 187
以下是 Java 代码实现:
```java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ScoreAnalysis {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("score.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("scoreAnalysis.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
int totalScore = Integer.parseInt(fields[1]) + Integer.parseInt(fields[2]) + Integer.parseInt(fields[3]);
writer.write(line + "," + totalScore + "\n");
}
reader.close();
writer.close();
}
}
```
运行该程序,会生成一个名为 scoreAnalysis.txt 的文件,内容如下:
```
张三,72,67,70,209
李四,92,98,88,278
王五,68,80,77,225
```
阅读全文