java读取日志,在Java中读取日志文件

Is it possible to read log files (abc.log) using java?

I want a specific string from my log file.

suppose this is the content of my logfile. I want the time stamp only (eg: 05:08:37) and print it the console.

2012-12-16 05:08:37,905 [Thread-1] INFO com.submit.SubmitService - Wait time 500

2012-12-16 05:08:38,444 [Thread-1] INFO com.submit.SubmitService - NO OF RECORDS TILL NOW 3755 TOTAL TIME -- << 539

2012-12-16 05:08:38,668 [Thread-1] INFO com.submit.SubmitService - Active Connection:: -69076

2012-12-16 05:08:38,670 [Thread-1] INFO com.submit.SubmitService - Active Connection:: -65764

解决方案

You can read your "log-file" as a normal file.

Then you can use, for instance, regular expression, to obtain the part of the string that you need:

try{

FileInputStream fstream = new FileInputStream("abc.log");

BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

String strLine;

/* read log line by line */

while ((strLine = br.readLine()) != null) {

/* parse strLine to obtain what you want */

System.out.println (strLine);

}

fstream.close();

} catch (Exception e) {

System.err.println("Error: " + e.getMessage());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值