weixin_52337976 2021-06-28 09:27 采纳率: 62.5%
浏览 13
已采纳

Java代码程序问题

 

  • 写回答

2条回答 默认 最新

  • 林林林林七 2021-06-28 10:16
    关注
    package com.io;
    
    import lombok.Data;
    
    import java.io.*;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     * @author LinQi
     * @version 1.0
     * @date 2021/6/28 9:52
     */
    @Data
    class Article {
        private Integer id;
        private String name;
        private Double prices;
        private Integer count;
    }
    
    class Demo {
        public static void main(String[] args) {
            List<Article> list = new ArrayList<>();
            String filePath = "d:/a.txt";
            File file = new File(filePath);
            BufferedReader reader = null;
            String tempString = null;
            String str = null;
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                while ((tempString = reader.readLine()) != null) {
                    str = tempString;
                    Article article = new Article();
                    String[] split = str.split(",");
                    article.setId(Integer.parseInt(split[0]));
                    article.setName(split[1]);
                    article.setPrices(Double.parseDouble(split[2]));
                    article.setCount(Integer.parseInt(split[3]));
                    list.add(article);
                }
                reader.close();
            } catch (
                    Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            List<Article> collect = list.stream().sorted(Comparator.comparing(Article::getPrices).reversed()).collect(Collectors.toList());
            File files = new File("d:/price_order.txt");
            try {
                if (!files.exists()) {
                    files.createNewFile();
                }
                OutputStream os = new FileOutputStream(files);
                for (Article article : collect) {
                    String st = article.getId()+","+article.getName()+","+ article.getPrices()+","+ article.getCount();
                    os.write(st.getBytes(StandardCharsets.UTF_8));
                    os.write(("\r\n").getBytes());
                }
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝批量检测实名工具
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题