How to Use Java to Read from a File That Is Actively Being Written To

How do I use Java to read from a file that is actively being written to?

Could not get the example to work using FileChannel.read(ByteBuffer) because it isn't a blocking read. Did however get the code below to work:

boolean running = true;
BufferedInputStream reader = new BufferedInputStream(new FileInputStream( "out.txt" ) );

public void run() {
while( running ) {
if( reader.available() > 0 ) {
System.out.print( (char)reader.read() );
}
else {
try {
sleep( 500 );
}
catch( InterruptedException ex ) {
running = false;
}
}
}
}

Of course the same thing would work as a timer instead of a thread, but I leave that up to the programmer. I'm still looking for a better way, but this works for me for now.

Oh, and I'll caveat this with: I'm using 1.4.2. Yes I know I'm in the stone ages still.

read from a file that is actively being written to using Java [Improvement]

The ideal improvement I am looking for is not to wait few seconds, but
for a certain event to happen => New data is added. Is this possible ?

Best solution : data pushing :

The application that produces the content should inform the other application as it may read the content.

You could use any channel that may convey the information between two distinct applications.

For example by using a specific file that the writer updates to notify new things to read.

The writer could write/overwrite in this file a update date and the reader would read the data file only if it doesn't read any content since this date.

A more robust way but with more overhead could be exposing a notification service from the reader side.

Why not a REST service.

In this way, the writer could notify the reader via the service as new content is ready.

Another thing to add, the whole program I am trying to write reads a
file in real-time and process its content. So this means that
thread.sleep or the timer will pause my whole program.

A workaround solution : data pulling performed by a specific thread :

You have probably a multi-core CPU.

So create a separate thread to read the produced file to allow other threads of your application to be runnable.

Besides, you also could performs some regular pause : Thread.sleep() to optimize the core use done by the reading thread.

It could look like :

Thread readingThread = new Thread(new MyReadingProcessing());
readingThread.start();

Where MyReadingProcessing is a Runnable :

public class MyReadingProcessing implements Runnable{

public void run(){
while (true){
readFile(...);
try{
Thread.sleep(1000); // 1 second here but choose the deemed reasonable time according the metrics of your producer application
}
catch(InterruptedException e){
Thread.currentThread().interrupt();
}
if (isAllReadFile()){ // condition to stop the reading process
return;
}
}
}
}


Related Topics



Leave a reply



Submit