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

Ensuring that a file is not currently being written to  RSS feed

 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I wonder if anyone of you guys out there could help me out with this:

I want to read a file, but only want to do that when another process
that is creating it is done creating it. Say it is an (external)
process A that creates the file and my process B that reads the file. I
want to somehow verify that when an attempt is made by B to read that
particular file in the file system, the file is not currently being
written to by process A.

A ----> writes ---> file <--- reads ---- B

Is there any way to do this in Java?

Many thanks!!!

Panagiotis.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I guess you can use the canRead and canWrite methods



// Mathias
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 1.4, there's no direct way to do what you want. I don't know if Java 5 is better.

If the process doing the writing can set the file write-only while writing it, you might be able to poll canRead() to determine when it is ready to be read.

Otherwise, you can poll the size of the file and wait for it to become stable. I've had to resort to that before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!