Java Scanner won't follow file
Posted
by Steve Renyolds
on Stack Overflow
See other posts from Stack Overflow
or by Steve Renyolds
Published on 2010-05-03T15:51:43Z
Indexed on
2010/05/03
16:38 UTC
Read the original article
Hit count: 340
Trying to tail / parse some log files. Entries start with a date then can span many lines.
This works, but does not ever see new entries to file.
File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");
while (true) {
while(src.hasNext()){
System.out.println("[ " + src.next() + " ]");
}
}
Doesn't seem like Scanner's next() or hasNext() detects new entries to file.
Any idea how else I can implement, basically, a tail -f with custom delimiter.
ok - using Kelly's advise i'm checking & refreshing the scanner, this works. Thank you !!
if anyone has improvement suggestions plz do!
File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");
while (true) {
while(src.hasNext()){
System.out.println("[ " + src.next() + " ]");
}
Thread.sleep(50);
if(bis.available() > 0){
src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");
}
}
© Stack Overflow or respective owner