Solved

Is the file being written this moment?

Posted on 2004-10-24
232 Views
Last Modified: 2010-07-27

Is there anyway of knowing that a particular file is being written this moment OR  is opened by some application with "write" previliges.
0
Question by:engineer007
    7 Comments
     

    Author Comment

    by:engineer007
    Explanation::

      I have two applications working on the same file. One application will write stuff on the file while the other one will copy the file to a different location and delete it from the original one. I executed a "file copy" procedure and was surprised to know that files can be copied to a different location at the same time they are being written. The result is "a corrupt file" at the copy location. So is there any way around u guys??
    0
     
    LVL 10

    Expert Comment

    by:NetworkArchitek
    Hi engineer007,
    Sure, the "file" object has two methods which return boolean: canRead() and canWrite(). You should be able to use this for your application.

    Cheers!
    0
     

    Author Comment

    by:engineer007
    Well no matter whether the file is being written orn't , canRead() and canWrite() both always return true. They aint seem to be doing my thing!!
    0
     
    LVL 24

    Accepted Solution

    by:
    There is 2 solutions to this problem:
    1) lock the file from the application that writes; have the other application(s) wait for the lock to be gone,
    2) trigger possible readers from the writer: there are numerous ways to do that.
    ;JOOP!
    0
     
    LVL 2

    Expert Comment

    by:tdisessa
    Calling the constructor of either FileWriter or FileOutputStream with the File/filename as an argument
    will throw an exception if the file is already opened for write by another process.

    Check out the Constructors for FileWriter or FileOutputStream.

     
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    OK.
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    Forced thanks.
    <*>
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This video teaches viewers about errors in exception handling.

    857 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now