qq_34436013 2016-06-07 09:22 采纳率: 28.6%
浏览 1577
已采纳

java 读取txt文件,一列日期(8位,如20160524),一列数值,将相同日期的数值求和并打印

图片说明

求前辈帮忙写下

  • 写回答

2条回答 默认 最新

  • little_how 2016-06-07 10:01
    关注

    public static void countDate(String filePath) {
    Map<String, Integer> map = new HashMap<String, Integer>();
    FileInputStream in = null;
    try {
    //filepath为文件路径
    in = new FileInputStream(filePath);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while(true) {
    //按行读取
    String str = br.readLine();
    if (str == null) break;
    if ((str = str.trim()).length() == 0) continue;
    Integer v = map.get(str);
    v = v == null ? 1 : v.intValue() + 1;
    map.put(str, v);
    }
    Iterator<String> iterator = map.keySet().iterator();
    while(iterator.hasNext()) {
    String key = iterator.next();
    System.out.println("date="+key+",totalCount="+map.get(key));
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (in != null) {
    try {
    in.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料