Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Ensuring that a file is not currently being written to

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I guess you can use the canRead and canWrite methods



// Mathias
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic