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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:28:50+00:00 2026-05-17T00:28:50+00:00

My code makes use of BufferedReader to read from a file [main.txt] and PrintWriter

  • 0

My code makes use of BufferedReader to read from a file [main.txt] and PrintWriter to write to a another temp [main.temp] file. I close both the streams and yet I was not able to call delete() method on the File object associated with [main.txt]. Only after calling System.gc() after closing both the stream was I able to delete the File object.

public static boolean delete (String str1, String str2, File FileLoc)
{
    File tempFile = null;
    BufferedReader Reader = null;
    PrintWriter Writer = null;

    try
    {
        tempFile = new File (FileLoc.getAbsolutePath() + ".tmp");
        Reader = new BufferedReader(new FileReader(FileLoc));
        Writer = new PrintWriter(new FileWriter(tempFile));
        String lsCurrLine = null;

        while((lsCurrLine = Reader.readLine()) != null)
        {
            // ...
            // ...

            if (true)
            {
                Writer.println(lsCurrLine);
                Writer.flush();
            }
        }

        Reader.close();
        Writer.close();
        System.gc();
    }
    catch(FileNotFoundException loFileExp)
    {
        System.out.println("\n File not found . Exiting");
        return false;
    }
    catch(IOException loFileExp)
    {
        System.out.println("\n IO Exception while deleting the record. Exiting");
        return false;
    }
}

Is this reliable? Or is there a better fix?

  • 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-17T00:28:51+00:00Added an answer on May 17, 2026 at 12:28 am

    @user183717 – that code you posted is clearly not all of the relevant code. For instance, those “…”‘s and the fact that File.delete() is not actually called in that code.

    When a stream object is garbage collected, its finalizer closes the underlying file descriptor. So, the fact that the delete only works when you added the System.gc() call is strong evidence that your code is somehow failing to close some stream for the file. It may well be a different stream object to the one that is opened in the code that you posted.

    Properly written stream handling code uses a finally block to make sure that streams get closed no matter what. For example:

    Reader reader = new BufferedReader(new FileReader(file));
    try {
        // do stuff
    } finally {
        try {
            reader.close();
        } catch (IOException ex) {
            // ...
        }
    }
    

    If you don’t follow that pattern or something similar, there’s a good chance that there are scenarios where streams don’t always get closed. In your code for example, if one of the read or write calls threw an exception you’d skip past the statements that closed the streams.

    Is this [i.e. calling System.gc();] reliable?

    No.

    1. The JVM may be configured to ignore your application’s gc() call.
    2. There’s no guarantee that the lost stream will be unreachable … yet.
    3. There’s no guarantee that calling System.gc() will notice that the stream is unreachable. Hypothetically, the stream object might be tenured, and calling System.gc() might only collect the Eden space.
    4. Even if the stream is found to be unreachable by the GC, there’s no guarantee that the GC will run the finalizer immediately. Hypothetically, running the finalizers can be deferred … indefinitely.

    Or is there a better fix ?

    Yes. Fix your application to close its streams properly.

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

Sidebar

Related Questions

I currently have 2 BufferedReader s initialized on the same text file. When I'm
I'm trying to make use of a java api for google voice (available here:
Okay, so I'm working on a project where I use a Java program to
I have a use case where a servlet (that has reasonably high concurrent use)
I am working on a personal project that uses a custom config file. The
I'm writing my first java client/server program which just establishes a connection with the
I've created a Java class that connects to an IIS website requiring NTLM authentication.
I've written a program that counts lines, words, and characters in a text: it
I'm trying to get a certain cookie in a java client by creating a
I am working with a unit-testing suite that hijacks function calls and tests expected

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.