import java.util.*;
import java.io.*;
public class MarksProcess {
public static void main (String[] args)throws Exception{
File inFile = new File ("hello.txt");
Scanner input = new Scanner(inFile);
while (input.hasNext()){
String line = input .nextLine();
System.out.println(line);
}
input.close();
}
}
我的文件输出是:
345493 Jim
123464 Pete
123234 Jay
如何使用学号对升级文本文件的输出进行排序,使其如下所示:
123234 Jay
123464 Pete
345493 Jim
答案 0 :(得分:1)
要订购结果,只需将它们添加到列表中,然后对此列表进行排序:
List<String> eachRows = new ArrayList<>();
while (input.hasNext())
eachRows.add(input.nextLine());
input.close();
Collections.sort(eachRows);
for(String s : eachRows)
System.out.println(s);
或者使用java 8:
eachRows.stream()
.sorted()
.forEach(row -> System.out.println(row));