weixin_43616382 2018-11-18 10:19 采纳率: 33.3%
浏览 582

java数据结构排序输出问题

请问这些数据 在txt文档中读取
然后分别按照升序和降序排列输出到新的txt文档中,ps:(新的文档中
要有两次排序的结果)
求大神给出完整java代码,小弟弄了好久了都,

s1109853011,JackieChan,CS001,85
s0907451244,FranklinPapadias,MA002,65
s0308893477,MirandaLambert,HM467,35
s4045128481,SophiaPam,CM604,75
s0812340023,PhilipsChiu,CS001,71

  • 写回答

2条回答

  • qq_43632454 2018-11-19 02:20
    关注

    不知道这个是不是你要的

     public class SortData {
        public static void main(String[] args) {
            String oldPath = "C:/Users/东海/Desktop/foo.txt";
            String newPath = "C:/Users/东海/Desktop/bar.txt";
            try {
                sortAndWriteData(oldPath, newPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void sortAndWriteData(String path, String newPath)
                throws IOException {
            ArrayList<HashMap<String, String>> list = getData(path);
            writeToText(list, newPath);
            Collections.sort(list, new Comparator<HashMap<String, String>>() {
    
                @Override
                public int compare(HashMap<String, String> o1,
                        HashMap<String, String> o2) {
                    if (o1.get("id").compareTo(o2.get("id")) > 0) {
                        return 1;
                    }
                    if (o1.get("id").compareTo(o2.get("id")) < 0) {
                        return -1;
                    }
                    return 0;
                }
    
            });
            writeToText(list, newPath);
    
        }
    
        private static void writeToText(ArrayList<HashMap<String, String>> list,
                String newPath) throws IOException {
            File file = new File(newPath);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
            StringBuilder stringBuilder = new StringBuilder();
            for (HashMap<String, String> hashMap : list) {
                String data=stringBuilder.append(hashMap.get("id")+",")
                        .append(hashMap.get("name")+",").append(hashMap.get("unknown")+",")
                        .append(hashMap.get("num")).toString();
                bufferedWriter.write(data+"\r\n");
                bufferedWriter.flush();
                stringBuilder.delete(0, stringBuilder.length());
            }
            bufferedWriter.close();
        }
    
        private static ArrayList<HashMap<String, String>> getData(String path)
                throws IOException {
            ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
            File file = new File(path);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String content = "";
            while (content != null) {
                content = bufferedReader.readLine();
                if (content == null) {
                    break;
                }
                String[] dataArray = content.split(",");
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", dataArray[0]);
                map.put("name", dataArray[1]);
                map.put("unknown", dataArray[2]);
                map.put("num", dataArray[3]);
                list.add(map);
                content = "";
            }
            bufferedReader.close();
            return list;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?