Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

file

file-io

I have an application that writes information to file. This information is used post-execution to determine pass/failure/correctness of the application. I'd like to be able to read the file as it is being written so that I can do these pass/failure/correctness checks in real time.

I assume it is possible to do this, but what are the gotcha's involved when using Java? If the reading catches up to the writing, will it just wait for more writes up until the file is closed, or will the read throw an exception at this point? If the latter, what do I do then?

My intuition is currently pushing me towards BufferedStreams. Is this the way to go?

like image 673
Anthony Cramp Avatar asked Aug 06 '08 23:08

Anthony Cramp


People also ask

Can you read a file that is being written to?

Most likely, you'll have to make your program open the file, read a small chunk, close the file, and then wait for a bit before reading again. Even then, there's no guarantee that you won't have the file open when the writing process tries to write.

How can we read data from a file in Java?

There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.

Can you read and write to the same file in Java?

You can't open the same file to read and write at the same time. You have to open and save the file information in a data structure and then close it. Then you have to work with the data structure in memory and open the file to write results. And when you finish to write you should close it.


2 Answers

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.

like image 122
Joseph Gordon Avatar answered Sep 23 '22 23:09

Joseph Gordon


If you want to read a file while it is being written and only read the new content then following will help you achieve the same.

To run this program you will launch it from command prompt/terminal window and pass the file name to read. It will read the file unless you kill the program.

java FileReader c:\myfile.txt

As you type a line of text save it from notepad and you will see the text printed in the console.

public class FileReader {      public static void main(String args[]) throws Exception {         if(args.length>0){             File file = new File(args[0]);             System.out.println(file.getAbsolutePath());             if(file.exists() && file.canRead()){                 long fileLength = file.length();                 readFile(file,0L);                 while(true){                      if(fileLength<file.length()){                         readFile(file,fileLength);                         fileLength=file.length();                     }                 }             }         }else{             System.out.println("no file to read");         }     }      public static void readFile(File file,Long fileLength) throws IOException {         String line = null;          BufferedReader in = new BufferedReader(new java.io.FileReader(file));         in.skip(fileLength);         while((line = in.readLine()) != null)         {             System.out.println(line);         }         in.close();     } } 
like image 43
tiger.spring Avatar answered Sep 22 '22 23:09

tiger.spring