Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:16:25+00:00 2026-05-27T22:16:25+00:00

My problem is this: I’m using a WatchService to get notified about new files

  • 0

My problem is this: I’m using a WatchService to get notified about new files in a specific folder, now if a file gets moved/copied or created in said folder an event gets triggered and the name of the new file gets returned. The problem now is, if I try to access the file and it is not fully there yet (e.g. the copy is still in progress) an exception gets raised. What i tried was to do something like this:

RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
FileChannel fc = raf.getChannel();
FileLock lck = fc.lock();

But even if a lock gets acquired, sometimes still an Exception gets raised if I try to write to the file because another process has still an open handle to it.

Now, how can a file in Java be locked for truly exclusive access?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:16:25+00:00Added an answer on May 27, 2026 at 10:16 pm

    For me, the statement

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

    returns a FileNotFoundException if I cannot acquire a lock on the file. I catch the filenotfound exception and treat it…

    public static boolean isFileLocked(String filename) {
        boolean isLocked=false;
        RandomAccessFile fos=null;
        try {
            File file = new File(filename);
            if(file.exists()) {
                fos=new RandomAccessFile(file,"rw");
            }
        } catch (FileNotFoundException e) {
            isLocked=true;
        }catch (Exception e) {
            // handle exception
        }finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
            }catch(Exception e) {
                //handle exception
            }
        }
        return isLocked;
    }
    

    you could run this in a loop and wait until you get a lock on the file. Wouldn’t the line

    FileChannel fc = raf.getChannel();
    

    never reach if the file is locked? You will get a FileNotFoundException thrown..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem crops up every now and then at work. Our build machine can
This problem has been kicking my butt for a few days now. I have
The problem this time is to get the median of three values (easy) I
my problem:--- This is my wifi_log.php file <?php // find out how many rows
I've got the following problem - This is my log4j config file: log4j.rootLogger=info, stdout,
I'm using a font in my site: http://www.fontsquirrel.com/fonts/Bentham But I have a problem: This
I have two CSS files, pic off the problem: this pick shows no caps
If I now look at the problem this way. The XAML code is as
I'm trying to get element by name, and I have a problem This is
Problem This question actually came up at work today. We are planning an experiment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.