0

I am supposed to create a log analysis software for my Master Degree thesis. Currently I am in the data collection phase :) so I need a way to read a file in real time. For example I have to monitor the "/var/log/auth.log" file and to look for failed logins (just an example among the 1000000s to come for my program:) ). So I need to read the file line by line. OK, if I were to use a scripting language (Python, Ruby) this would be really easy, but since I am in JAVA, I wasn't very lucky. Anybody gotta clue how to read a file in Real Time, maybe an example? Thanks in advance.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user2435860
  • 778
  • 3
  • 9
  • 22
  • Do you mean you want to tail the file? –  Apr 08 '14 at 13:29
  • Exactly, like tail. I've already done some research for this. I could use something like execve with tail, but it would not be useful, because I must call it constantly, and it is impossible to know how many lines were written during calls. – user2435860 Apr 08 '14 at 13:31

2 Answers2

2

This sounds duplicated to Java IO implementation of unix/linux "tail -f"

And the winner was:

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/input/Tailer.html

Community
  • 1
  • 1
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
1
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while(true) {
    line = reader.readLine(); // blocks until next line available
    // do whatever You want with line
}
azgar
  • 93
  • 6
  • Is there any way to open the file for reading and place directly at the end of the file? This is precisely my problem. I do not know how to place myself at the end of the file,and read the lines to come, starting that moment. – user2435860 Apr 08 '14 at 13:46