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