循环遍历文件夹,将文件中相邻两个时间戳大于3600的统计出来写入txt文件中

 @Test
    public void newtest5() throws IOException {
      //遍历文件夹并获取路径
        File dir = new File("C:/Users/lenovo/Desktop/抓拍时间");
        File [] files = dir.listFiles();
        String newDir = "C:/Users/lenovo/Desktop/抓拍时间统计";
        for(File file : files){
            test(file.getAbsolutePath(), newDir + file.getName());
        }

    } 
public void test(String path, String newpath) throws IOException {
        BufferedWriter bufferedWriter = null;
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
            bufferedWriter = new BufferedWriter(new FileWriter(newpath));
            String line ;
            String line1;
            long line3 = 0;
            while ((line = bufferedReader.readLine())!=null){
                long time1 = Long.valueOf(line);
               if((line1 = bufferedReader.readLine())!=null) {
                long time2 = Long.valueOf(line1);
                   if(time2-time1>3600){
                       line3 = time2 - time1 ;
                       String time11 = formatEpochSecond("yyyy-MM-dd_HH:mm:ss",time1);
                       String time22 = formatEpochSecond("yyyy-MM-dd_HH:mm:ss",time2);
                       long line33 = line3/60;
                       bufferedWriter.write(time11+" , "+ time22 +" , "+line33+"\r\n");
                   }
               }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            bufferedWriter.close();
        }
    }
   //将时间戳转换为时间
 public static String formatEpochSecond(String pattern, long epochSecond) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond),
                ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

猜你喜欢

转载自blog.csdn.net/qq_39082699/article/details/88966170
今日推荐