aspose file tools*
The moose likes Java in General and the fly likes How to ensure a file is written completely. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to ensure a file is written completely." Watch "How to ensure a file is written completely." New topic
Author

How to ensure a file is written completely.

Avi Nash
Ranch Hand

Joined: Jul 17, 2003
Posts: 69
Hi
I want to know how can i make sure whether a file is written completely; since I need to read this file.
I have a command which executes a command using Runtime.exec(String command).
This command writes two files to a specified folder. Now I want to read these files from this folder. But sometimes when I try reading these files, the files might not yet been written completely, and I get error because of this.
So how/when can I be sure of whether the file is completely written. As of now Iam check if the file exists() (using File Class -> exists() method)
Thx in advance.
rgds
Avinash
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have you tried to wait for the process writing the files to complete?
Avi Nash
Ranch Hand

Joined: Jul 17, 2003
Posts: 69
Hi David
Thx for the immediate reply.
I have tried waitFor(); but the problem is that the process is not getting terminated.
Should not the process get terminated automatically once the command is executed? Or should we terminate it using a separate command, if any.
The code:
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(commandToExecute);
process.waitFor();
System.out.println
("Exitvalue="+process.exitValue ());
is just waiting and not coming out.
rgds
Avinash
[ July 24, 2003: Message edited by: Avi Salimath ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

What does the called process do? Your Java code looks right to me, so it strikes me that the problem must be with the process you are trying to run. If it goes into some loop or some wait state, then there might be some issues.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I did some trial and error with exec() and wound up hooking streams to the proc's stdin, stdout, errout and waiting until all three streams closed to know the command was well and truly done. Here is my code and some notes about it. It's a little over-complicated for your needs because it also does sockets to a remote machine.
Another trick we talked about recently with FTP is to write a THIRD file from your process. When the process opens it to start writing you can see it on disk and you know the first two are finished. You never actually read the third one, so you don't care when it finishes. You'd have to poll in a loop to spot the third file.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to ensure a file is written completely.