• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to ensure a file is written completely.

 
Avi Nash
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to wait for the process writing the files to complete?
 
Avi Nash
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic