This question isn't really a Java-Linux question, but I'll answer
anyway since I have the code right here.
>I can run a process with something like "tail -f /var/log/messages"
>and getInputStream... There's a way to do this, only with java?
I've taken this code out of a bigger package, so it won't compile as
is, but should contain enough info for you to make this happen. It
works for me in Windows and Linux.
I realize now we should be using a StringBuffer, not a String.
// main loop - test the file's length to see if it's changed, then sleep.
// This seems awfully ugly and expensive, but it's the way GNU tail -f works.
public void run() {
File file;
lastLength = file.length();
long thisLength = file.length();
if (thisLength != lastLength) { // aha, a change!
if (thisLength == 0) { // 0 can mean file is gone
if (file.exists() == false)
return;
} else if ( thisLength < lastLength) {
lastLength = thisLength; // file got smaller -- not
System.out.println("shorter!"); // exactly sure what to do
} else { // normal case, file growing
String read = new String(""); // the string we will store it in
try {
FileInputStream fis = new FileInputStream(file); // open it to read
fis.skip(lastLength); // skip ahead to the final position of the last
time around
byte[] reading = new byte[1024]; // seems like a good length for
reading
while ( fis.available() > 0 ) { // read until done
if ( fis.available() > reading.length )
fis.read(reading, 0, reading.length);
else
fis.read(reading, 0, fis.available()); // this will prevent it
from blocking
read += new String(reading); // create the string
}
fis.close();
} catch (IOException error) {
return;
}
lastLength = thisLength;
System.out.println(read);
}
}
}
[EMAIL PROTECTED]
. . . . . . . . http://www.media.mit.edu/~nelson/
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]