Ensuring that a file is not written to

V

varlagas

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.
 
E

Eric Sosman

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?

Not that I know of. One technique that's often used
is to have A write to "output.tmp", say, and rename it to
"output.dat" when it has finished all its writing. That
way, B cannot even find the "output.dat" file while A is
still writing it.
 
T

tiewknvc9

how about this.

Make another file called flag.txt
set the text of flag.txt text to "1" when the program is creating it,
then set the flag.txt text to "0" when it is done creating it.

Before process B reads the file, check its associated flag.

you can create many flag.txt files, you could simply use the
strNameOfTheFile + "flag.txt" as the unique flag.txt documents per
file.

let me know how it goes for you.
 
E

EJP

tiewknvc9 said:
how about this.

Make another file called flag.txt
set the text of flag.txt text to "1" when the program is creating it,
then set the flag.txt text to "0" when it is done creating it.

Before process B reads the file, check its associated flag.

you can create many flag.txt files, you could simply use the
strNameOfTheFile + "flag.txt" as the unique flag.txt documents per
file.

let me know how it goes for you.

I prefer the renaming method, but if you do the above you don't need the
0/1 contents of 'flag.txt', you can just check for its presence or absence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top