Using Scanner and FileReader to read from a file in real time

Hi

I would like to read continually from a text file (which is continually being appended to) and display each line as it appears in the textfile (in a text area).

What I would like to know is do I have to continually reopen the file (using FileReader) and buffer it into a Scanner everytime I want to read the changes made to it? There must be a better way.

Much appreciation for any help.

[416 byte] By [msparka] at [2007-11-27 11:47:43]
# 1

I think you have to reopen your file every time you want to update your textarea.

Why you don't use a database? You can then read the last row which has been recently updated.

java_2006a at 2007-7-29 18:14:54 > top of Java-index,Java Essentials,New To Java...
# 2

I tried to reopen the file in a while loop, after scanner has read the last line. But if I it seems that if I amend the file and resave it using a seperate application, the new line isn't read. Here's the relevant code:

while(showScreen == true){

try {

fs = new Scanner(new BufferedReader(new FileReader(outFile)));

fs.useDelimiter("\\r");

} catch(IOException e){

System.out.println("Problems opening file. IO exception: " + e.toString());

return;

}

if (fs!=null){

//Call method to read and display each line in textArea

readAndDisplay();

fs.close();

fs = null;

}

//Wait half a second before checking if there is anymore text

try {

wait(500);

} catch (InterruptedException e){

System.out.println("File reading interrupted");

return;

}

}// while

Any ideas as to why this doesn't work?

msparka at 2007-7-29 18:14:54 > top of Java-index,Java Essentials,New To Java...