IO流查找文件然后写入TXT文档

时间:2021-07-13 20:59:19

今天领导让分析日志,把日志中所有登录过的员工信息都拿出来。于是。把日志摘下来谢了这段代码

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new FileReader("D:" + File.separator + "access.log.2017-09-12"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:"
+ File.separator + "2.txt"));

String line = null;
Set<String> set = new HashSet<String>();
while ((line = bufferedReader.readLine()) != null) {
int temp = line.indexOf("user_work_num");
if(temp>0){
String lt = line.substring(temp+14, temp+22);
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(lt);
if( isNum.matches() ){

if(!set.contains(lt)){

bufferedWriter.write(lt);
bufferedWriter.newLine();
bufferedWriter.flush();
}
set.add(lt);
}
}
}
bufferedReader.close();
bufferedWriter.close();
}
}