对时间戳分类,并将规定时间段内的时间戳个数统计出来写入txt文件中

本文介绍了一个Java程序,用于比较两个文本文件中的时间戳数据,并将指定时间范围内的计数汇总到新文件中。通过读取旧文件的时间戳,程序将其按每分钟划分并统计数量,然后写入新文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 @Test
    public void compare()  {
        String oldPath = "C:/Users/lenovo/Desktop/136.txt";
        String newPath = "C:/Users/lenovo/Desktop/111.txt";
       try {
           BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newPath));
           BufferedReader bufferedReader = new BufferedReader(new FileReader(oldPath));
           String line;
           //将起始时间以及该时间段内的时间戳个数放入map中
           Map<Long, Integer> map = new HashMap<>();
           long start = 1552968199;
           long code = 60000;
           long changeLine = 0;
           int sum = 0;
           while ((line = bufferedReader.readLine())!= null){
               changeLine = Long.valueOf(line);
               while (changeLine!=0){
                   if((changeLine>=start)&&(changeLine<(start+code))) {
                       sum = ++sum;
                       map.put(start, sum);
                       changeLine = 0;
                   }else {
                       start = start + code;
                       sum = 0;
                   }
               }
           }
           for (Long time : map.keySet()){
               String time11 = formatEpochSecond("yyyy-MM-dd_HH:mm:ss",time);
               bufferedWriter.write(time11+"\t"+ map.get(time) +"\r\n");
           }
               bufferedWriter.flush();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
//时间戳转换为时间
 public static String formatEpochSecond(String pattern, long epochSecond) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond),
                ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值