缓冲流+实例(恢复文件顺序)

1,BufferinputStream-字节
`public class helloWorldOutPut {
public static void main(String[] args) {

    //1.首先创建字符缓冲输入流与字节缓冲输出流
    try (
            BufferedReader r1 = new BufferedReader(new FileReader("D:\\zhaogongzuo\\code\\untitled\\src\\heima01.txt"));
            BufferedWriter w1 = new BufferedWriter(new FileWriter("D:\\zhaogongzuo\\code\\untitled\\src\\heima02.txt"));
    ){
        //2.我要建立一个集合能存这些数据
        List<String> l1 =new ArrayList<>();

        //3.要遍历这个文件,然后加到集合里进行操作
        String line;
        while((line= r1.readLine()) != null){
            l1.add(line);
        }
        //4.对list集合中的每一段文章进行排序
        Collections.sort(l1);
        //5.现在需要写入到新文件中
        for (String s : l1) {
            w1.write(s);
            //字节缓冲输出流换行
            w1.newLine();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

}`

posted @ 2024-03-12 23:02  heshuiqingan  阅读(15)  评论(0)    收藏  举报