第一个任务的要求,将.log文件的内容读取并处理。
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * 读取LOG文件并进行处理,并生成相应的xml文件 * * @author wyd * */ public class Reader { /** * 读取log文件<br> * 根据传入的文件路径逐行获取文件内容,并将文本作为集合返回 * * @param filePath * @return */ public List<String> readLog(String filePath) { List<String> list = new ArrayList<String>(); try { FileInputStream fis = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line; try { while ((line = br.readLine()) != null) { //去掉不相关的字段(most) if (line.trim().equals("") || line.trim().startsWith("HQx-MDS"))//这里过滤所有不想要的东西 continue; else list.add(line); } } catch (IOException e) { e.printStackTrace(); System.out.println("读取一行数据时出错"); } } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("文件读取路径错误FileNotFoundException"); } return list; }
}
【记一笔:过滤那部分应该写成一个方法,将需要过滤的参数写入配置文件,这样就可以实时修改了。】