用java读取txt文件,然后对数据进行排序,去重等操作。

Id Name Year Current Cumulative
16 Melissa Will 3 5 9
17 Naomi Thomas 3 3 12
21 Ronaldo Gomes 3 1 11
22 Sam Del-Prete 2 4 10
2 Abe Storey 3 4 6
3 Anthony Tabrin 3 1 7
18 Nathan Bentley 3 2 19
把姓名首字母相同的过滤;4、5列求和、求平均值;按第一列排序;写到新的txt文件中。
因为各氏姿个列没有分隔符,我添加了逗颤段号作分隔符.文件内容如下.
Id Name Year Current Cumulative
16, Melissa Will, 3, 5, 9
17, Naomi Thomas, 3, 3, 12
21, Ronaldo Gomes, 3, 1, 11
22, Sam Del-Prete, 2, 4, 10
2, Abe Storey, 3, 4, 6
3, Anthony Tabrin, 3, 1, 7
18, Nathan Bentley, 3, 2, 19

你用这个文件内容,试一下下边的程序.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("H:\\test.txt");
BufferedReader br = new BufferedReader(fr);
int row = 1;

int sum1 = 0, sum2 = 0;
FileWriter fw = new FileWriter("H:\\new.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Id Name Year Current Cumulative");
bw.newLine();
List<String> first = new ArrayList<String>();
while (br.ready()) {
if (row++ == 1) {
br.readLine();
continue;
}
String line = br.readLine();
String[] array = line.split(",", 5);
String tmp = array[1].trim().substring(0, 1);
if (!first.contains(tmp)) {
sum1 += toInt(array[3].trim());
sum2 += toInt(array[4].trim());
bw.write(line);
bw.newLine();
first.add(tmp);
}
}

bw.write("sum : \t\t\t\t" + sum1 + "\歼洞绝t" + sum2);
bw.newLine();
bw.write("average : \t\t\t" + (sum1 / (first.size() * 1.0)) + "\t"
+ sum2 / (first.size() * 1.0));
br.close();
fr.close();
bw.close();
fw.close();
}

public static int toInt(String str) {
try {
return Integer.parseInt(str);
} catch (Exception e) {
return 0;
}
}

}
把姓名首字母过滤什么意思?
是指把姓名首字母相同的行的4,5列进行求和和平均值吗?