How does RandomFileAccess lock at the OS level?

Posted on 2006-03-30
Last Modified: 2008-03-10
If I open a filed using the RandomFileAccess command and "rw", will the OS stop the file from being written to by any other program?  will a subsequent call using "rw" fail?  

Would this differ between windows and *nix?
Question by:jjacksn
    LVL 86

    Accepted Solution

    >>will the OS stop the file from being written to by any other program?

    I don't think you can guarantee that. Test it though
    LVL 5

    Author Comment

    Ug.... I can't seem to do this simple thing:  

    in the simplest case, I want to open a file what to open a file such that no one else can write to it while it is open... I know you can do this in windows, at least.  

    However, calling

    raf = new RandomAccessFile(fileName, "rw");
    raf = new RandomAccessFile(fileName, "rw");

    works... so that's not going to do it.  does the normal File object?
    LVL 86

    Expert Comment

    LVL 92

    Assisted Solution

    FileLock's aren't going to achieve what u need.
    You could rename the file to a temporary file anem before you open it for writing, and then rename it back once done. That would stop anyone else accessing the file.
    LVL 5

    Author Comment

    objects, that's great thanks.
    I do know, at least in windows, that I've been looking at a file, and then tried to right to it, and it was "locked" by another program.  I don't mind if someone is reading the old data, so long as that don't to write to it until the first method call finished.  Can this be accomplished?  
    LVL 30

    Assisted Solution

    If the file has been opened for editing by any process, I don't think the OS will let another process modify it (whatever kind of file it is)?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    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 …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now