This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I would like to ask that, how to show "writing in a text file" on consoles ?
i.e. if i am writing some content in a file say abc.txt using a java program, then while writing in a file, how can i show updated file contents on other console (ie. other than my currently running java program console).
I want to do two things simultaneously: 1. Updating contents of a file using java program 2. Showing upadated contents each time (when a line gets written to a file) on another console. (I have already opened one extra console to show write operation in a file).
I am not supposed to use any applet or awt component.Which APIs would are expected to suffice?
This is the legendary producer-consumer scenario but in different processes!! You can use java.nio.channels.FileLock to simulate the same. You can have a lock file that the two processes race to grab. A very crude sample code is as follows: (DISCLAIMER: The following code is just a POC that file locks can be used to do the required.)
Note: 1) The above producer waits for the user input to write to a file. 2) Thread.sleep() in producer is to allow the consumer to grab the lock. If this wait is not provided then its not necessary that for the small period that the producer releases the lock, the consumer will be able to grab it.