• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to show writing in a text file.

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thank You,

Regards,
Amit Pandit.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)

Producer:


Consumer:



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.
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

the other console which i use, doesn't means for other java program
That console is just to show a file which is being get updated by a
java program from first console.

My final o/p would be -

program1.java |
console 1 | console 2
// File abc.txt | File abc.txt is always shown here
get updated here | simultaneoulsy

Thanks

Amit Pandit
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is this console all about. I mean which application opens this console.
I really cant understand this console paradigm. :roll:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a UNIX-like system, use "tail -f <filename>" to look at the file; as the Java program appends new data to the file, the "tail" program will see the new data and display it.

Or you could open the file in Emacs, and use "auto-refresh" mode, which will automatically update a buffer periodically.

On Windows, who knows.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic