wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes How to write in text/doc file dynamiclly ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to write in text/doc file dynamiclly ?" Watch "How to write in text/doc file dynamiclly ?" New topic
Author

How to write in text/doc file dynamiclly ?

mozammil muzza
Ranch Hand

Joined: Dec 22, 2011
Posts: 32

Hi All,
I am opening one text file (e.g. write.txt) using runtime and process class and again reading from some other text file (e.g. read.txt) line by line and then trying to write in same opened file i.e. write.txt. but here only i am facing problem. It is writer fine but i am not able to see this in opened file.
My Requirement is to see that text which thread is writing from other file i.e. read.txt, but it is not happening.
That's why i am saying this as dynamically.
Hope you all get my problem.
Please help to achieve this.

Note : For all of this i am using the thread which is scheduled for every 2 mins.

Thanks,
Muzza


Khuda Haafiz Muzza 4 Java
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

mozammil muzza wrote:Hi All,
I am opening one text file (e.g. write.txt) using runtime and process class and again reading from some other text file (e.g. read.txt) line by line and then trying to write in same opened file i.e. write.txt. but here only i am facing problem. It is writer fine but i am not able to see this in opened file.
My Requirement is to see that text which thread is writing from other file i.e. read.txt, but it is not happening.
That's why i am saying this as dynamically.
Hope you all get my problem.
Please help to achieve this.


Not a thread question. Maybe I should move this to the IO forum, but since the question is not well formed, let's look at it here.

Java IO is not that low level, and it is buffered. You can't have a reader instantaneous see the changes as you write them. There may be a way to implement it, but something tells me that is has to do with check the modification times, and reopening the file -- not sure.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It's a dangerous thing to read and write a file from two processes (or two threads of the same process) simultaneously. A better solution would be to read the contents of the file into an appropriate data structure in memory, and access that in a properly synchronized manner. You could flush the data structure to disk after each change if that's important.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write in text/doc file dynamiclly ?