?
Solved

how to check if a file is being written to in java

Posted on 2002-06-14
2
Medium Priority
?
367 Views
Last Modified: 2008-03-06
I need to perform some operation on a file (e.g. move or delete), but NOT when it is currently being written to by some other process.

I believe this is easily done in "C".  Is there JAVA equivalent? (I checked java.io.File; it does not have what I need).  I would like to do this in pure JAVA, and not use JAVA/c.


0
Comment
Question by:happyloman
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 600 total points
ID: 7078440
I believe you would be able to determine whether a file is being written to, by trying to open it. Since writing is an inherently exclusive access operation, another process wouldn't be able to open it, so the open attempt should fail.

However, if the other "writing" process can happen at random, you can still have a problem in that in the time between your test and your operation (move or delete) the writing process could start up.......
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7078948
>>
I believe you would be able to determine whether a file is being written to, by trying to open it. Since
writing is an inherently exclusive access operation, another process wouldn't be able to open it, so
the open attempt should fail.
>>

This, I think, is true if you substitute 'move' or 'delete' for 'open' in the above sentence.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question