这个代码是啥啊代码是啥

 

好容易🧐

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Txt {
    public static void main(String[] args) {
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            //创建file对象,指定文件路径
            File file = new File("D:\\a.txt");

            //创建文件流开始读文件
            isr = new InputStreamReader( new FileInputStream(file) );
            br = new BufferedReader( isr );

            //创建StringBuffer对象,用来下面拼接读取出来的字符串串
            StringBuffer sb = new StringBuffer();
            String str = null;

            //新建List,存放每个Coffee对象,用来下面排序用
            List<Coffee> list = new ArrayList<>();

            //开始读文件,当br.readLine()为null时,读取结束
            while( (str = br.readLine() ) != null){
                //将每次读取出来的str拼到sb后面
                sb.append( str + "\r\n" );

                //创建数组,用来存放读取出的字符串每个信息
                String[] haha = str.split(",");

                //将信息分别set到Coffee对象内
                Coffee c = new Coffee(haha[0], haha[1], Double.parseDouble(haha[2]), Integer.parseInt(haha[3]));
                list.add(c);
            }

            //输出读取到的内容
            System.out.println("读取到的字符串:" + "\r\n" + sb.toString());

            System.out.println("------------------------------");

            for ( Coffee coffee : list ) {
                System.out.println("排序前:" + coffee.getNo() + "-" + coffee.getName() + "-" + coffee.getPrice() + "-" + coffee.getCount() );
            }

            //利用List的sort属性对list中每个coffee对象的价格进行排序,x.getPrice()在前是由低到高排序
            list.sort((x, y) -> Double.compare(y.getPrice(), x.getPrice()));

            System.out.println("------------------------------");

            //新建StringBuffer对象sb0,用来拼装排序后字符串,后面写入文件
            StringBuffer sb0 = new StringBuffer();
            for ( Coffee coffee : list ) {
                System.out.println("排序后:" + coffee.getNo() + "-" + coffee.getName() + "-" + coffee.getPrice() + "-" + coffee.getCount() );
                //将每次循环得到的结果,拼接到sb0对象中
                sb0.append(coffee.getNo() + "," + coffee.getName() + "," + coffee.getPrice() + "," + coffee.getCount() + "\r\n");
            }

            //新建file对象,并指定要写入的文件的位置
            File file1 = new File("D:\\price_order.txt");
            //创建文件输出流写入文件
            FileOutputStream fop = new FileOutputStream(file1);
            if ( !file1.exists() ) {
                //判断文件是否存在,不存在就创建
                file1.createNewFile();
            }
            //得到sb0对象的字符集合,开始写文件
            byte[] xixi = sb0.toString().getBytes();
            fop.write( xixi );
            fop.flush();
            fop.close();

            System.out.println("------------------------------");

            System.out.println("文件写入成功,程序结束");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件未找到");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败");
        } finally {
            //最后关闭流释放内存
            try {
                //先开后闭,后开先闭
                br.close();
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

//创建Coffee对象用来封装上面读取文件时每条商品信息
class Coffee implements Serializable {
    //序号
    private String no;
    //名称
    private String name;
    //价格
    private double price;
    //数量
    private int count;

    //get   set方法
    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    //全参构造器
    public Coffee(String no, String name, double price, int count) {
        this.no = no;
        this.name = name;
        this.price = price;
        this.count = count;
    }
}