all,
i have log file below content.
request centercord. 2010-12-14 12:42:13.724 [ 6796] **************************** 2010-12-14 12:42:13.724 [ 6796] 1111111111111111 2010-12-14 12:42:13.724 [ 6796]22222222222 response centercord. 2010-12-14 12:42:21.802 [ 5960] 11111111111111 2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff 2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt request centercord. 2010-12-14 12:42:13.724 [ 6796] ****************************
i need create 2 log files 1 storing request details , other 1 storing response details. how can parse , prepare 2 log files?.
i need below answer.
log 1: request centercord. 2010-12-14 12:42:13.724 [ 6796] **************************** 2010-12-14 12:42:13.724 [ 6796] 1111111111111111 2010-12-14 12:42:13.724 [ 6796]22222222222 2010-12-14 12:42:13.724 [ 6796] **************************** log 2: response centercord. 2010-12-14 12:42:21.802 [ 5960] 11111111111111 2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff 2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt
regards, kanagaraj
here how it:
import java.io.*; import java.util.scanner; public class test { public static void main(string[] args) { try { printwriter requests = new printwriter("requests.txt"); printwriter responses = new printwriter("responses.txt"); printwriter currentlog = null; scanner s = new scanner(new file("log.txt")); while (s.hasnextline()) { string line = s.nextline(); if (line.startswith("request from")) currentlog = requests; else if (line.startswith("response from")) currentlog = responses; else if (currentlog != null) currentlog.println(line); } requests.close(); responses.close(); s.close(); } catch (ioexception ioex) { // handle exception... } } }
given log.txt
request centercord. 2010-12-14 12:42:13.724 [ 6796] **************************** 2010-12-14 12:42:13.724 [ 6796] 1111111111111111 2010-12-14 12:42:13.724 [ 6796]22222222222 response centercord. 2010-12-14 12:42:21.802 [ 5960] 11111111111111 2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff 2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt request centercord. 2010-12-14 12:42:13.724 [ 6796] ****************************
it produces, requests.txt
2010-12-14 12:42:13.724 [ 6796] **************************** 2010-12-14 12:42:13.724 [ 6796] 1111111111111111 2010-12-14 12:42:13.724 [ 6796]22222222222 2010-12-14 12:42:13.724 [ 6796] ****************************
...and responses.txt
2010-12-14 12:42:21.802 [ 5960] 11111111111111 2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff 2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt
Comments
Post a Comment