聊天记录按时间排序,时间相同时按人名排序



/*
 * OrderMsg
 * 2014/11/4
 * Created by wowo
 */
/**
 *现有一个文件unorderedmsg.txt,内容是一段被打乱的聊天记录。
 *请按时间恢复聊天记录顺序,时间一样时按人名排序,结果输出到orderedmsg.txt。
 *并统计每个人的说话次数,人名和次数之间使用4个空格隔开,输出到count.txt中。
 *输出的文件都放置在classpath下。
 */

import java.io.*;
import java.util.*;

public class OrderMsg {
    Map<String, Integer> map = new HashMap<String, Integer>();/*存放名字和次数*/
    SortedMap<String, String> map2 = new TreeMap<String, String>();/*TreeMap自动进行字典序排序*/

    public static void main(String[] args) throws Exception {
        OrderMsg om = new OrderMsg();
        String url = OrderMsg.class.getResource("/").getFile();
        String path1 = url + "unorderedmsg.txt";
        String path2 = url + "orderedmsg.txt";
        String path3 = url + "count.txt";


        om.oputOderedmsg(path1, path2);/*排序并输出到文件orderedmsg.txt*/
        om.oputcount(path1, path3);/*统计并输出到count.txt*/
    }


    public void oputOderedmsg(String path1, String path2) throws Exception {
    /*输出到orderedmsg.txt*/
        File f = new File(path1);
        searchFile(f, "排序");
        output(path2);
    }

    public void oputcount(String path1, String path3) throws Exception {
    /*输出到count.txt*/
        File f = new File(path1);
        searchFile(f, "统计");
        output2(path3);
    }

    public void searchFile(File f, String s) throws Exception {
    /*查找文件*/
        if (f.isFile()) {
            if (s.equals("排序"))
                order(f);
            else if (s.equals("统计"))
                count(f);
        } else if (f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null) {
                for (File file : files) {
                    searchFile(file, s);
                }
            }
        }
    }


    public void order(File f) throws Exception/*排序*/ {
        try {
            FileInputStream fis = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String tmp = "";
            while ((tmp = br.readLine()) != null) {
                String[] s = null;
                s = tmp.split("    ");
                String time = s[1];/*提取时间*/
                String name = s[0];/*提取名字*/
                String tn = time.concat(name);/*字符串连接*/
                if (map2.get(tn) == null) {
                    map2.put(tn, tmp);
                }
            }
            br.close();
            fis.close();
        } catch (Exception e) {
            System.out.print("排序出错!");
            e.printStackTrace();
        }
    }


    public void count(File f) throws Exception {/*统计*/
        {
            try {
                FileInputStream fis = new FileInputStream(f);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                String tmp = "";

                while ((tmp = br.readLine()) != null) {
                    String[] s = null;
                    s = tmp.split("    ");
                    String name = s[0];/*提取名字*/
                    if (map.get(name) == null) {
                        map.put(name, 1);
                    } else {
                        int value = map.get(name);
                        map.put(name, value + 1);

                    }
                }
                br.close();
                fis.close();
            } catch (Exception e) {
                System.out.print("统计出错!");
                e.printStackTrace();
            }
        }
    }

    public void output(String path2) throws Exception {/*结果输出到orderedmsg.txt*/
        try {

            FileWriter fw = new FileWriter(path2);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            Iterator<String> iter = map2.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                pw.println(map2.get(key));
            }
            pw.close();
            bw.close();
            fw.close();
        } catch (Exception e) {
            System.out.print("输出到orderedmsg出错!");
            e.printStackTrace();
        }
    }

    public void output2(String path3) throws Exception {/*结果输出到count.txt*/
        try {
            FileWriter fw2 = new FileWriter(path3);
            BufferedWriter bw2 = new BufferedWriter(fw2);
            PrintWriter pw2 = new PrintWriter(bw2);
            Iterator<String> iter2 = map.keySet().iterator();
            while (iter2.hasNext()) {
                String key = iter2.next();
                pw2.println(key + "    " + map.get(key));
            }
            pw2.close();
            bw2.close();
            fw2.close();
        } catch (Exception e) {
            System.out.print("输出到count出错!");
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值