Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader not reading on-the-fly populated .txt file  RSS feed

 
Dave McNamara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I currently have 2 Java programs that I want to run separately and the link between them is a .txt file. One writes to the file and the other program reads the lines in the files and does a task.

The issue I am having is that I start the 2 of them and it takes 5 to 6 minutes for program 1 to populate the .txt file. So this means I have program 2 in an while loop.

(.txt file is empty when program 2 starts).

From reading what "contine" does in Java, the program will sit in that while loop until a new line of text is available to be read in the file.

But the bufferedreader is not picking up this new line even though program 1 is calling the .close() method after it has written to the file.

What I would like to happen is 2 programs start. Program one does its business and then outputs to .txt file.
Program 2 sits there waiting for lines to appear in the text file and when they do, do something with them.

At the moment, program 2 is just sitting there and wont work unless I wait until program 1 has written to the file and then start program 2. I need to be able to execute and forget for a few hours as the task takes time.

Any ideas please?

Thanks,
Dave
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave McNamara wrote:Hi,

I currently have 2 Java programs that I want to run separately and the link between them is a .txt file. One writes to the file and the other program reads the lines in the files and does a task.

The issue I am having is that I start the 2 of them and it takes 5 to 6 minutes for program 1 to populate the .txt file. So this means I have program 2 in an while loop.

(.txt file is empty when program 2 starts).

From reading what "contine" does in Java, the program will sit in that while loop until a new line of text is available to be read in the file.

But the bufferedreader is not picking up this new line even though program 1 is calling the .close() method after it has written to the file.

What I would like to happen is 2 programs start. Program one does its business and then outputs to .txt file.
Program 2 sits there waiting for lines to appear in the text file and when they do, do something with them.

At the moment, program 2 is just sitting there and wont work unless I wait until program 1 has written to the file and then start program 2. I need to be able to execute and forget for a few hours as the task takes time.

Any ideas please?

Thanks,
Dave


Your code will loop like crazy and eat all CPU available. You should at least put a Thread.sleep(1000) in your loop to avoid that.

If you insist on using files to communicate between your programs, you will find out that it is quite low level and complex to implements in order to prevent any kind of wrong runtime behavior. You will probably end up with a second empty file which is going to be used to notify the first program that data is ready + a lock file to make sure no concurrency problems occur.

One solution could be to use java.net.DatagramPacket for your first program to notify the second one that data is ready in the file. There are other solutions, tcp sockets, message queuing (MQ), etc.

If the first program is only going to write once to the data file or if the second program is always done processing data before the first program writes to the file again, simply having your first program writing an empty flag file when it is done writing the data file could do the trick for you without any need for file locking.

Your second program could just loop until it finds the flag file, read the datafile and delete the flag file. Do not forget to use Thread.sleep(1000) in your loop although in order to prevent CPU starving.

If you are only trying to tail a log file and you are on Unix, you could just use:
tail -f <your_data_file> | java SecondProgram
and have your SecondProgram read from System.in

Otherwise you will basically have to implement the same in Java here is some hints, apparently, apache commons has a tailer:
https://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f
https://www.informit.com/guides/content.aspx?g=java&seqNum=226

Just Google for:
"tail -f" in java

quote the "tail -f" in your search query

Good luck!





 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!