java - Reading a log file which gets rolled over -


i trying use simple program read log file. code used follows:

randomaccessfile in = new randomaccessfile("/home/hduser/documents/sample.txt", "r"); string line; while(true) { if((line = in.readline()) != null) { system.out.println(line); } else { thread.sleep(2000);  

the code works new lines being added log file not replicate rollover process. i.e. when content of log file cleared expect java console continue reading text first line newly written log. possible? changes need made existing code achieve that?

i sorry... bad.. don't want go blank.. want next new line written log read.

since need able read beginning when file cleared, need monitor length of file , reset cursor pointer when length of file reduces. can reset cursor using seek(..) method.

see code below -

randomaccessfile in = new randomaccessfile("/home/hduser/documents/sample.txt", "r"); string line; long length = 0;//used check file length while (true) {     if(in.length()<length){//new condition reset position if file length reduced          in.seek(0);     }     if ((line = in.readline()) != null) {         system.out.println(line);         length = in.length();     } else {         thread.sleep(2000);     } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -