BufferedReader.readLine() pb !!! Help !!!

Hello,

I want to read lines from a file "while (true)" ... because an other application will append some lines in this file at the same tim I will read lines from it.

This code is bad because the readLine() is not blocking when no input is available and then it is consuming all CPU process:

BufferedReader in = new BufferedReader(new FileReader("readfromfile.txt"));

String readLine;

while (true)

{

readLine = in.readLine();

if (readLine!=null) // Doing something with readLine ...

}

Someone could help and tell to me if a similar method does exist with a blocking readLine() ?

Thanks

Ludo from france

[689 byte] By [ludovicmaillet] at [2007-9-30 16:07:36]
# 1
A sidenote: not every platform will make it simultaneously possible to keep a file open for reading for one process and writing for another.Anyway, what about Thread.sleep-ing some time in the loop as a work-around? // Salutations a France.
BIJ001 at 2007-7-5 23:37:51 > top of Java-index,Administration Tools,Sun Connection...
# 2

ok

I'm thinking about this solution, with a Thread.sleep :

BufferedReader in = new BufferedReader(new FileReader("readfromfile.txt"));

String readLine;

while (true)

{

while (!in.ready())

{

try

{

Thread.sleep(1000);

}

catch (InterruptedException interruptExcept)

{

}

}

readLine = in.readLine();

if (readLine!=null) // Doing something with readLine ...

}

..... but I would prefer not to waste time :-|

Any idea ?

ludovicmaillet at 2007-7-5 23:37:51 > top of Java-index,Administration Tools,Sun Connection...
# 3
Use Thread.yield() rather than sleep(); that way you won't waste as much time.I think you should rather sleep (or yield) when readLine==null because in.ready() may return false before the end of a file has been reached...
jsalonen at 2007-7-5 23:37:51 > top of Java-index,Administration Tools,Sun Connection...
# 4
You might like to have a look at java.nio.channels. I think these classes address this issue specifically.See if FileChannel is helpful.
steevcoc at 2007-7-5 23:37:51 > top of Java-index,Administration Tools,Sun Connection...