tags:

views:

142

answers:

4

All,

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. Is this reliable? Or is there a better fix ?

  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;
      }
  }
A: 

try using the apache commons io

http://commons.apache.org/io/description.html

Aaron Saunders
Why isn't File.delete() working ?
letronje
Yes It doesn't seem to delete the file even after closing all the streams
darkie15
+1  A: 

try using java.io.File library. here the simple sample:

File f = new File("file path or file name");
f.delete();
fritz
`File.delete()` isn't working, that's the problem in the question.
Colin Hebert
+1  A: 

When you say you "close both the streams" you mean the BufferedReader and the PrintWriter?

You should only need to close the BufferedReader before the delete will work, but you also need to close the underlying stream; normally calling BufferedReader.close() will do that. It sounds like you think you are closing the stream but you aren't actually succeeding.

One problem with your code: you don't close the streams if exceptions occur. It's usually best to close the streams in a finally block.

Also, the code you posted doesn't use File.delete() anywhere? And what exactly do the ... lines do - are they re-assinging Reader to a new stream by any chance?

davmac
Posted is the code above...
darkie15
+1  A: 

@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.

Stephen C