Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Knowing when another process has finished writing to file

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We have the following situation:
- Some (non-java) process is uploading files to some directory.
files are huge (several Giga), and size is *not* known in advance.
- We need java to monitor this directory, and tell when a file has finished uploading (so the entire file is ready on disk).

Is there a way to do it ?
Some things I've tried , that didn't work:
- Checking File.canWrite() (hoped that if another process is writing to file, java would get canWrite=false). No luck.
- Opening a FileInputStream (or nio's FileChannel) and waiting for EOF. No luck either (if the other process as written, say, 1/2 the file, then java would read half the file and then report EOF, even though the other process hasn't finished writing).

Any ideas would be appreciated (even if anyone knows of a platform-dependent solution, e.g one that only works on Windows or Unix, it would still help).

Thanks very much.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you initiate the file writing process from within java?

using something like Runtime.exec("process_name");
if u can, then it is possible to wait until that process has finished
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a common problem and one without a great answer I think.

If you list the directory for new files every "n" minutes you can wait until a file has the same size & date two times in a row as a clue that the other process has *probably* stopped writing.

If you can modify the other process it's a neat trick to have it write the ginormous file and then write another tiny file to let you know the big one is done.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can modify the other process it's a neat trick to have it write the ginormous file and then write another tiny file to let you know the big one is done.


yep..i would go for tht one.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the replies.

Unfortunately, I can't control the writing process (it's an FTP server, "out of the box")...
Guess i'll have to go with "wait until size doesn't change".

Thanks again.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!