文件排序小笔记

目的为从文件中读取数据做排序:

结果:

文件排序小笔记

入参

A文件:

文件排序小笔记

B文件:

文件排序小笔记

代码:

   //存活动id号
        List<String> acionlist = new ArrayList<>();
        //存放数据
        Map<String,List<String>> map = new HashMap<>();

        BufferedReader bufferedReader = new BufferedReader(new FileReader("acionID.txt"));
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("acionID1.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("acionID.dat"));
        String len = null;
        String len1 = null;
        while ((len = bufferedReader.readLine()) != null) {
            acionlist.add(len);
        }
        bufferedReader.close();

        while ((len1 = bufferedReader1.readLine()) != null) {
            ListIterator<String> stringListIterator = acionlist.listIterator();
            while (stringListIterator.hasNext()) {
                String vaule = stringListIterator.next();
                map.put(len1,acionlist);
            }
        }
        bufferedReader1.close();
        long timeStart = System.currentTimeMillis();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            for (String v : entry.getValue()) {
//                System.out.println(entry.getKey()+"---->"+v);
                bufferedWriter.write(entry.getKey()+","+v);
                //换行
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
        }
        bufferedWriter.close();
        long timeEnd = System.currentTimeMillis();
        System.out.println("总共花费:" + (timeEnd - timeStart) + "ms");
        System.out.println("写入完毕!");

开发哥们给的提示:

 List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        Map<String,List<String>> map = new HashMap<>();

        map.put("1",list);
        map.put("2",list);
        map.put("3",list);
        map.put("4",list);

        for(Map.Entry<String,List<String>> m : map.entrySet()){
            for(String v : m.getValue()){
                System.out.println(m.getKey()+"---->"+v);
            }
        }

java改进版本:

long timeStart = System.currentTimeMillis();
        BufferedReader activity_ID = new BufferedReader(new FileReader("activity_ID.txt")); //活动di
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("acionID.dat"));
        String len = null;
        String len1 = null;
        while ((len = activity_ID.readLine()) != null) {
            BufferedReader cookice_id = new BufferedReader(new FileReader("cookice_id.txt"));
            while ((len1 = cookice_id.readLine()) != null) {
                bufferedWriter.write(len + "," + len1);
                bufferedWriter.newLine();
            }
            cookice_id.close();
        }
        activity_ID.close();
        bufferedWriter.close();
        long timeEnd = System.currentTimeMillis();
        System.out.println("总共花费:" + (timeEnd - timeStart) + "ms");
        System.out.println("操作完毕!");
    }

文件排序小笔记
文件排序小笔记

python使用时间:

start_time = time.time()
    f = open("a.txt", "r")
    f1 = open("b.txt", "r")
    f2 = open("c.txt", "w")

    # for i in range(10):
    #     f1.write(str(i) + '\n')
    # f1.close()
    for i in f.read().split():
        for j in f1.read().split():
            f2.write(i+","+j+"\n")
            # print(i+","+j+"\n")
        f1.seek(0)
    f.close()
    f1.close()
    f2.close()
    end_time = time.time()
    print("使用多少时间:!%s"%(end_time-start_time))

文件排序小笔记

转载于:https://blog.51cto.com/357712148/2347821

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值