[Logo]
Forums Register Login
How to ensure a file is written completely.
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
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);
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 ]
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.
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss


This thread has been viewed 2045 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 19, 2018 11:40:35.