开发者

How to read a log file and parse the entries in Java?

开发者 https://www.devze.com 2023-03-18 22:11 出处:网络
I have silly question, but I\'m unprofessional. I am trying to make a logs reader in Java, which will read the logs from directory. It must recognizes errors,informations and warnings. Which solution

I have silly question, but I'm unprofessional. I am trying to make a logs reader in Java, which will read the logs from directory. It must recognizes errors,informations and warnings. Which solution will be t开发者_高级运维he best(the fastest) in this case: FileInputStream with BufferedReader or FileReader with BufferedReader? Or maybe something else.


BufferedReader in = new BufferedReader(new FileReader("log.txt"));
String line1 = in.readLine();

is about as fast as you can get. BufferedReader buffers your input so it's faster than simply using FileReader. And in case you were wondering, Scanner is significantly slower than BufferedReader.


For choosing between a FileInputStream or a FileReader, it depends on what you want to work with - bytes (FileInputStream) or text / characters 'a', 'b', ... (FileReader). If you have the log files as plain text, it would make sense to use a FileReader.

Note that a FileReader uses an encoding, read the JavaDoc.

0

精彩评论

暂无评论...
验证码 换一张
取 消