Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic

how to check a file is writing or finished  RSS feed

 
Jimmy Chen
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Report post to moderator
I want to check a file if it is being written by other programs or has finished.

I used "file.canRead() && file.canWrite()", but it doesn't work. Anybody can give me some suggestion?

thanks in advance.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Report post to moderator
I don't think there's any easy way, if there isn't some specific
feature in the file format you can discern. What I usually do
is write files to a temp directory and after that succeeds, the
writing app renames the file to the proper target directory.
That way, if another app can see it, it's all there. This is a good
idea in any case, because if the writing app crashes half-way
through, the garbage file is in temp only.
 
Jimmy Chen
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks,Jeff.but I can't do in that way.
My way I want to do now is to check the file size. If it is increasing in size, it is being written. I'm not sure if it works, just try
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
Another thing you can try is use the tryLock() method in FileChannel. If it returns null, that means another program has a lock on the file, which probably means it's still being used. If it does not return null, that probably means the other programs are not using the file anymore. This isn't really guaranteed, because it depends on how well the operating system and the other programs enfore and/or respect the locking mechanism. But there's a fair chance it will work exactly right on your system. So I'd say it's worth a try.

Alternately - what are these other programs anyway? And what's in the file? Is it possible that you can read the contents of the file and determine whether it's complete or not? Maybe there's something that's supposed to occur at the end of the file, and you can check if it's there or not? Or you can count the number of records to see if they're all there? There are all sorts of thigns that might be possible for you to do here, depending on how this file is being used, and what's in it.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
If it is increasing in size, it is being written.
True, but that isn't helpful. If the file size is not increasing in size, that could mean the writing program has paused for a moment, or is filling a large buffer, or that the operating system has not posted the writes that the program did. I have not had much success (and several failures) with that technique. Nowadays I use the technique that Jim described, which is to have the sending program rename the file when it's complete.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
That was Jeff. Though I would prefer that method too if at all possible.
 
Jimmy Chen
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you all, let me try it and find a proper way out
 
Eung maeng
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi YU,
Did you find nay good solution? I have exactly same situation to figure out.
if you have one, please leave here.

thanks,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
143
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
"Eung JM", please check your private messages for an important administrative matter.

Also please do not repeat a question in more than one topic, and please don;t revive old topics.
[ July 31, 2008: Message edited by: Bear Bibeault ]
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!