Forums Register Login
How to ensure a file is written completely.
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.
Have you tried to wait for the process writing the files to complete?
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);
("Exitvalue="+process.exitValue ());
is just waiting and not coming out.
[ July 24, 2003: Message edited by: Avi Salimath ]
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.
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.
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Become a Java guru with IntelliJ IDEA

This thread has been viewed 2403 times.

All times above are in ranch (not your local) time.
The current ranch time is
Mar 23, 2019 07:24:19.