/**
* 读取日志,找出嫌疑人
*
* @param logFile 日志文件
* @throws
*/
@Test
public void testLog() {
List<String> paths = getFileName("C:\\Users\\cowain\\Desktop\\新建文件夹");
for (int i = 0; i < paths.size(); i++) {
File tmpLogFile = new File(paths.get(i));
try {
//指定文件可读可写
final RandomAccessFile randomFile = new RandomAccessFile(tmpLogFile, "r");
String tmp = "";
String pre = "";
while ((tmp = randomFile.readLine()) != null) {
String sb=new String();
String txt = new String(tmp.getBytes("ISO8859-1"));
if(txt.contains("CWA2179")){
sb+=System.getProperty("line.separator");
sb+=pre;
sb+=System.getProperty("line.separator");
sb+=txt;
try {
String path = "C:\\Users\\cowain\\Desktop\\王总日志.txt";
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(path,true)));
out.write(sb+" ");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
pre=txt;
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("结束");
}
private List<String> getFileName(String filePath){
List<String> list=new ArrayList<>();
if(null!=filePath&&!"".equals(filePath)){
File file = new File(filePath);
//判断文件或目录是否存在
if(!file.exists()){
log.info("【"+filePath + " not exists】");
}
//获取该文件夹下所有的文件
File[] fileArray= file.listFiles();
File fileName = null;
for(int i =0;i<fileArray.length;i++){
fileName = fileArray[i];
list.add(fileName.getAbsolutePath());
}
}
return list;
}
分析日志.log
最新推荐文章于 2022-11-28 22:25:25 发布