2)从文件中读取各学生成绩,计算所有学生成绩的平均值,将成绩的最大值、最小值及平均值并存入文本文件中。 (3)将上面两个文本文件的内容合并到一个新的文本文件中。 2.统计一个文本文件中字母'A'和'a'出现的总次数。
时间: 2024-02-13 11:59:36 浏览: 70
第一题:
1. 从文件中读取各学生成绩:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("data.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
double sum = 0;
int count = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(" ");
double score = Double.parseDouble(parts[2]);
sum += score;
count++;
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}
double average = sum / count;
// 将成绩的最大值、最小值及平均值存入文本文件
FileWriter writer = new FileWriter("result.txt");
writer.write("最大值:" + max + "\n");
writer.write("最小值:" + min + "\n");
writer.write("平均值:" + average + "\n");
writer.close();
bufferedReader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 将上面两个文本文件的内容合并到一个新的文本文件中:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
// 读取成绩文件
FileReader reader1 = new FileReader("data.txt");
BufferedReader bufferedReader1 = new BufferedReader(reader1);
String line;
double sum = 0;
int count = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
while ((line = bufferedReader1.readLine()) != null) {
String[] parts = line.split(" ");
double score = Double.parseDouble(parts[2]);
sum += score;
count++;
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}
double average = sum / count;
bufferedReader1.close();
reader1.close();
// 将成绩的最大值、最小值及平均值存入文本文件
FileWriter writer1 = new FileWriter("result.txt");
writer1.write("最大值:" + max + "\n");
writer1.write("最小值:" + min + "\n");
writer1.write("平均值:" + average + "\n");
writer1.close();
// 读取字母统计文件
FileReader reader2 = new FileReader("letter_count.txt");
BufferedReader bufferedReader2 = new BufferedReader(reader2);
// 将两个文件的内容合并到一个新文件中
FileWriter writer2 = new FileWriter("merged.txt");
while ((line = bufferedReader2.readLine()) != null) {
writer2.write(line + "\n");
}
writer2.write("\n");
writer2.write("成绩统计结果:\n");
writer2.write("最大值:" + max + "\n");
writer2.write("最小值:" + min + "\n");
writer2.write("平均值:" + average + "\n");
writer2.close();
bufferedReader2.close();
reader2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
第二题:统计一个文本文件中字母'A'和'a'出现的总次数
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("text.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
int countA = 0;
int counta = 0;
String line;
while ((line = bufferedReader.readLine()) != null) {
for (char c : line.toCharArray()) {
if (c == 'A') {
countA++;
}
if (c == 'a') {
counta++;
}
}
}
FileWriter writer = new FileWriter("letter_count.txt");
writer.write("A出现次数:" + countA + "\n");
writer.write("a出现次数:" + counta + "\n");
writer.close();
bufferedReader.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文