计算log文件的开始结束时间差

  1 import java.io.BufferedReader;
  2 import java.io.BufferedWriter;
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileReader;
  6 import java.io.FileWriter;
  7 import java.io.IOException;
  8 import java.io.RandomAccessFile;
  9 import java.util.ArrayList;
 10 import java.util.regex.Matcher;
 11 import java.util.regex.Pattern;
 12 
 13 
 14 public class TimeCount {
 15 
 16     public static void main(String[] args) throws IOException {
 17         // TODO Auto-generated method stub
 18         //读取具体文件每一行的内容
 19         String content;
 20         //Job_Objectのlist
 21         ArrayList<Job> JobObjecttList = new ArrayList<Job>();
 22         //需要分析文件所在的文件夹
 23         String filePath ="C:\TimeCount\in\";
 24 //        String filePath =args[0];
 25         File file = new File(filePath);
 26         //判断文件或目录是否存在
 27         if(!file.exists()){
 28             System.out.println("「"+filePath + " not exists」");
 29             
 30         }
 31         //获取该文件夹下所有的文件
 32         File[] fileArray= file.listFiles();
 33         File fileName = null;
 34         
 35         for(int i =0;i<fileArray.length;i++){
 36             fileName = fileArray[i];
 37             //判断此文件是否存在
 38             if(fileName.isDirectory()){
 39                 System.out.println("「"+fileName.getName()+" not exists」");
 40                 
 41             }else{
 42                 BufferedReader bfr = new BufferedReader(new FileReader(filePath+fileName.getName()));
43 Job j = new TimeCount().new Job(); 44 // Job j = new Job(); 45 //正序读取,并将文件名,开始时间放入对象 46 while((content = bfr.readLine()) != null) { 47 //正则表达式匹配到符合时间格式的那一行 48 if(content.matches(".*\d{4}\/\d{2}(\/\d{2}).*")) { 49 //正则表达式匹配到符合时间格式的字符串 50 Pattern pt=Pattern.compile("\d{4}\/\d{2}(\/\d{2})"); 51 Matcher mt=pt.matcher(content); 52 while(mt.find()) { 53 int a = mt.start()+11; 54 // System.out.println(content.substring(a,a+12)); 55 56 j.setJobId(fileName.getName()); 57 //截取这一行的时分秒的字符串,并保存到Job对象中 58 j.setJobTimeS(content.substring(a,a+12)); 59 break; 60 } 61 break; 62 } 63 } 64 //倒序读取,并将结束时间放入对象 65 readReverse(j,filePath+fileName.getName(),"SJIS"); 66 JobObjecttList.add(j); 67 bfr.close(); 68 69 // System.out.println(fileName.getName()); 70 } 71 } 72 //写结果文件 73 BufferedWriter bfw = new BufferedWriter(new FileWriter("C:\TimeCount\out\result.txt")); 74 // BufferedWriter bfw = new BufferedWriter(new FileWriter(args[1])); 75 //头部标题 76 String contentResultStr ="JobID JobStartTime JobEndTime TimeDiff"; 77 bfw.write(contentResultStr); 78 bfw.write(" "); 79 String contentResult =""; 80 int timeDiff = 0; 81 //循环输出Job对象的list中的内容 82 for(Job j:JobObjecttList) { 83 System.out.println(j.getJobId() + " "+j.getJobTimeS()+" "+j.getJobTimeE()); 84 85 timeDiff = timeCount(j.getJobTimeS(),j.getJobTimeE()); 86 contentResult = j.getJobId()+" "+j.getJobTimeS()+" "+j.getJobTimeE()+" "+timeDiff; 87 bfw.write(contentResult); 88 bfw.write(" "); 89 } 90 bfw.close(); 91 } 92 93 /* 94 * 倒序读取,并将结束时间放入对象 95 * */ 96 public static void readReverse(Job j,String filename, String charset) { 97 RandomAccessFile rf = null; 98 try { 99 String content; 100 rf = new RandomAccessFile(filename, "r"); 101 long fileLength = rf.length(); 102 long start = rf.getFilePointer();// 返回此文件中的当前偏移量 103 long readIndex = start + fileLength -1; 104 String line; 105 rf.seek(readIndex);// 设置偏移量为文件末尾 106 int c = -1; 107 while (readIndex > start) { 108 c = rf.read(); 109 if (c == ' ' || c == ' ') { 110 line = rf.readLine(); 111 if (line != null) { 112 content = new String(line.getBytes("ISO-8859-1"),charset); 113 if(content.matches(".*\d{4}\/\d{2}(\/\d{2}).*")) { 114 Pattern pt=Pattern.compile("\d{4}\/\d{2}(\/\d{2})"); 115 Matcher mt=pt.matcher(content); 116 while(mt.find()) { 117 int a = mt.start()+11; 118 // System.out.println(content.substring(a,a+12)); 119 j.setJobTimeE(content.substring(a,a+12)); 120 break; 121 } 122 break; 123 } 124 // System.out.println(new String(line.getBytes("ISO-8859-1"),charset)); 125 } else { 126 // System.out.println(line); 127 } 128 readIndex--; 129 } 130 readIndex--; 131 rf.seek(readIndex); 132 // if (readIndex == 0) {// 当文件指针退至文件开始处,输出第一行 133 // System.out.println(rf.readLine()); 134 // } 135 } 136 } catch (FileNotFoundException e) { 137 e.printStackTrace(); 138 } catch (IOException e) { 139 e.printStackTrace(); 140 } finally { 141 try { 142 if (rf != null) 143 rf.close(); 144 } catch (IOException e) { 145 e.printStackTrace(); 146 } 147 } 148 } 149 150 /* 151 * 计算每个job的开始结束时间差 152 * */ 153 public static int timeCount(String timeStr,String timeEnd) { 154 int timeStr_Hours = Integer.parseInt(timeStr.substring(0, 2)); 155 int timeEnd_Hours = Integer.parseInt(timeEnd.substring(0, 2)); 156 int timeStr_mins = Integer.parseInt(timeStr.substring(3, 5)); 157 int timeEnd_mins = Integer.parseInt(timeEnd.substring(3, 5)); 158 int timeStr_seconds = Integer.parseInt(timeStr.substring(6, 8)); 159 int timeEnd_seconds = Integer.parseInt(timeEnd.substring(6, 8)); 160 int result = (timeEnd_Hours-timeStr_Hours)*3600 161 +(timeEnd_mins-timeStr_mins)*60 162 +(timeEnd_seconds-timeStr_seconds); 163 return result; 164 } 165 166 /** 167 * Job对象 168 * */ 169 class Job{ 170 private String jobId; 171 private String jobTimeS; 172 private String jobTimeE; 173 public String getJobId() { 174 return jobId; 175 } 176 public void setJobId(String jobId) { 177 this.jobId = jobId; 178 } 179 public String getJobTimeS() { 180 return jobTimeS; 181 } 182 public void setJobTimeS(String jobTimeS) { 183 this.jobTimeS = jobTimeS; 184 } 185 public String getJobTimeE() { 186 return jobTimeE; 187 } 188 public void setJobTimeE(String jobTimeE) { 189 this.jobTimeE = jobTimeE; 190 } 191 } 192 193 }
原文地址:https://www.cnblogs.com/maol986162214/p/15353258.html