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 594077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:54:53+00:00 2026-05-13T15:54:53+00:00

I’m trying to delete a file that another thread within my program has previously

  • 0

I’m trying to delete a file that another thread within my program has previously worked with.

I’m unable to delete the file but I’m not sure how to figure out which thread may be using the file.

So how do I find out which thread is locking the file in java?

  • 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-13T15:54:53+00:00Added an answer on May 13, 2026 at 3:54 pm

    I don’t have a straight answer (and I don’t think there’s one either, this is controlled at OS-level (native), not at JVM-level) and I also don’t really see the value of the answer (you still can’t close the file programmatically once you found out which thread it is), but I think you don’t know yet that the inability to delete is usually caused when the file is still open. This may happen when you do not explicitly call Closeable#close() on the InputStream, OutputStream, Reader or Writer which is constructed around the File in question.

    Basic demo:

    public static void main(String[] args) throws Exception {
        File file = new File("c:/test.txt"); // Precreate this test file first.
        FileOutputStream output = new FileOutputStream(file); // This opens the file!
        System.out.println(file.delete()); // false
        output.close(); // This explicitly closes the file!
        System.out.println(file.delete()); // true
    }
    

    In other words, ensure that throughout your entire Java IO stuff the code is properly closing the resources after use. The normal idiom is to do this in the try-with-resources statement, so that you can be certain that the resources will be freed up anyway, even in case of an IOException. E.g.

    try (OutputStream output = new FileOutputStream(file)) {
        // ...
    }
    

    Do it for any InputStream, OutputStream, Reader and Writer, etc whatever implements AutoCloseable, which you’re opening yourself (using the new keyword).

    This is technically not needed on certain implementations, such as ByteArrayOutputStream, but for the sake of clarity, just adhere the close-in-finally idiom everywhere to avoid misconceptions and refactoring-bugs.

    In case you’re not on Java 7 or newer yet, then use the below try-finally idiom instead.

    OutputStream output = null;
    try {
        output = new FileOutputStream(file);
        // ...
    } finally {
        if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
    }
    

    Hope this helps to nail down the root cause of your particular problem.

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

Sidebar

Ask A Question

Stats

  • Questions 467k
  • Answers 468k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The first thing you might want to try is using… May 16, 2026 at 2:12 am
  • Editorial Team
    Editorial Team added an answer As seen in the jQuery source, $('#id') does simply document.getElementById,… May 16, 2026 at 2:12 am
  • Editorial Team
    Editorial Team added an answer File specification should be single quotes. Try: CREATE TABLESPACE CTTT444_tbs… May 16, 2026 at 2:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.