I have a log file which is increasing always due to logging for different logging event in my application. Now i want to check some patterns in the log file and if I find any match for the patterns I will print it some where else. The log file is not static file it is always increasing and I my program should not check the old line again. It should always checks the new line which have not been checked yet.
Asked
Active
Viewed 50 times
0
-
["_incriminating_"](https://dictionary.cambridge.org/dictionary/learner-english/incriminating) who? Or is it whom? – Boris the Spider Sep 24 '16 at 12:08
-
It is increasing by the different places of the application where I used to log an event. – ARUP BAROI Sep 24 '16 at 12:12
-
Is it totally necessary to do this in Java? If you command a command like you could do this easily with `tail` and `grep` – tddmonkey Sep 24 '16 at 12:26
-
Have a look at logstash https://www.elastic.co/products/logstash – Michel Feldheim Sep 24 '16 at 12:46
-
not necessarily in java . In any other ways works then fine – ARUP BAROI Sep 24 '16 at 12:47
1 Answers
0
Store the position of what you have checked up to in some kind of integer or file. now when you look for a pattern look for a regular expression and then print it to whatever output stream.
First read the file into an array like so, call the getWordsFromFile() method.
/**
*
* @param fileName is the path to the file or just the name if it is local
* @return the number of lines in fileName
*/
public static int getLengthOfFile(String fileName) {
int length = 0;
try {
File textFile = new File(fileName);
Scanner sc = new Scanner(textFile);
while (sc.hasNextLine()) {
sc.nextLine();
length++;
}
} catch (Exception e) {
}
return length;
}
/**
*
* @param fileName is the path to the file or just the name if it is local
* @return an array of Strings where each string is one line from the file
* fileName.
*/
public static String[] getWordsFromFile(String fileName) {
int lengthOfFile = getLengthOfFile(fileName);
String[] wordBank=new String[lengthOfFile];
int i = 0;
try {
File textFile = new File(fileName);
Scanner sc = new Scanner(textFile);
for (i = 0; i < lengthOfFile; i++) {
wordBank[i] = sc.nextLine();
}
return wordBank;
} catch (Exception e) {
System.err.println(e);
System.exit(55);
}
return null;
}
For larger files see this question Then once you load your String[], you can iterate through like so.
for(int i=0;i<arr.length;i++)
doSomething(arr[i]);
to something like
for(int i= start;i<arr.length;i++)
doSomething(arr[i]);

Community
- 1
- 1

Rohan Jhunjhunwala
- 416
- 5
- 19
-
But how can i avoid rechecking the previously check lines? Line number I can save but it have to check each line then only can find the last read line which is not what i want. – ARUP BAROI Sep 24 '16 at 12:17
-
@ARUPBAROI I need to see your code, but assuming you know how to read a file into an array. modify it from for(int i=0;i
– Rohan Jhunjhunwala Sep 24 '16 at 12:18 -
I think normal file reading will not work. I think you are telling me using a buffer reader and check for each line. But I want when next time I start reading the file It should start counting from the last line which it has already read previous execution. – ARUP BAROI Sep 24 '16 at 12:21
-
@ARUPBAROI why will normal file reading not work, but other than that, what you are saying is correct. – Rohan Jhunjhunwala Sep 24 '16 at 12:23
-
Is there ant API which can do the purpose with very less time? I want to avoid fileInputStream reading. – ARUP BAROI Sep 24 '16 at 12:28
-
-
For a log file of size in GB's it will be a performance issue.The code you have written is ok for small size file which even I know. But here I am talking about file size over 10 GB. – ARUP BAROI Sep 24 '16 at 13:03
-
-
-