一.原文件
二.代码
package org.example;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class day12 {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("d:/temp/day05/出师表.txt"));
BufferedWriter bf = new BufferedWriter(new FileWriter("d:/temp/day05/新出师表.txt"))
) {
//定义ArrayList集合存储内容
List<String> data = new ArrayList<>();
//按行读取
String line;
while ((line = br.readLine()) != null) {
data.add(line);
}
//排序
Collections.sort(data);
System.out.println(data);
//遍历集合内容,写出到新文件
for (String s : data) {
bf.write(s);
bf.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
三.结果