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

  • Home
  • SEARCH
  • 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 6787995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:25:56+00:00 2026-05-26T17:25:56+00:00

I am aware that this question is a raging duplicate of this question. However

  • 0

I am aware that this question is a raging duplicate of this question. However, I have now read that entire page twice, and some sections 3 times, and for the life of me I don’t see how/where it is answered!

So, on to my problem.

I am at work and am stuck using Java 6 SE and cannot upgrade to 7. I am writing a program that creates a file, writes to it, does some processing, and then needs to delete the file out of existence. I am having the exact same problem as the person who asked the question I reference above: Java will not delete the file and I cannot figure out why.

The code:

File f = null;
FileWriter fw = null;
try
{
    f = new File("myFile.txt");
    fw = new FileWriter(f);
    fw.write("This is a sentence that should appear in the file.");
    fw.flush();
    if(f.delete())
        System.out.println("File was successfully deleted.");
    else
        System.err.println("File was not deleted.");
}
catch(Exception exc)
{
    System.err.println(exc.getMessage());
}
catch(Error er    {
    System.err.println(er.getMessage());
}
catch(Throwable t)
{
    System.err.println(t.getMessage());
}
finally
{
    fw.close();
}

It is not throwing any throwables, errors or exceptions (I included those to rule out any and all edge cases). The second print statement ("File was not deleted.") is being printed to the console. I am running this on Windows 7 and am writing to a folder where I have full permissions (rwx).

The user asking the question I referenced answered his own question, but does so (in my humble opinion) in a not-so-straight-forward way. In any case, I am having trouble making sense of it. He/she seems to be alluding to the fact that using a BufferedReader as opposed to a FileInputStream made the difference for him/her, but I just don’t see how that applies.

Java 7 seems to have fixed this issues with the introduction of a java.nio.file.Files class, but again, I can’t use Java 7 for reasons outside the scope of my control.

Other answerers to that referenced question allude that this is a “bug” in Java, and give all sorts of circumventions, such as explicitly calling System.gc(), etc. I have tried all of these and they are not working.

Maybe someone can add a fresh perspective and jog some thinking for me.

  • 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-26T17:25:56+00:00Added an answer on May 26, 2026 at 5:25 pm

    You’re trying to delete() a file which is still referenced by an active, open FileWriter.

    Try this:

    f = new File("myFile.txt");
    fw = new FileWriter(f);
    fw.write("This is a sentence that should appear in the file.");
    fw.flush();
    fw.close(); // actually free any underlying file handles.
    if(f.delete())
        System.out.println("File was successfully deleted.");
    else
        System.err.println("File was not deleted.");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm aware that this question may have been asked before, but I still haven't
I'm aware that variants of this question have been asked before, but I do
I'm aware that this question has been asked before. I have looked through them
I aware that this will be a less programming question, but still... How can
In the answer to this question ovanes states: Please be aware that boost::lexical_cast is
In this question , I was made aware of glob()'s GLOB_BRACE option that allows
I'm aware that MD5 has had some collisions but this is more of a
I am aware that this is not a direct 'development' question but I need
i'm very aware that this is probably not a stackoverflow question, but i figure
Yes, I'm aware that this question has already been post, but ... I'm looking

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.