Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File deletion with delete() method in Java

Tags:

java

file

I have a little doubt about following code:

try {
    File file = new File("writing");
    file.createNewFile();
    System.out.println(file.delete());
    System.out.println(file.exists());

    PrintWriter pw = new PrintWriter(file);
    pw.print(324.2342);
    pw.flush();
    pw.close();
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    System.out.println(br.readLine());
    br.close();
} catch(IOException ioE) {
    System.out.println("Indeed");
}

Why in this circumstance the method file.delete() apparently says that it works as it returns "true" when executed and it gets also confirmed by the file.exists() method which returns "false". However at runtime I do not get any exception like "IOException the file "writing" does not exist" or something likewise.

Why does the file keep staying in the heap even though deleted physically? Shouldn't it be automatically garbage collected as soon as the delete method gets called? I know it does not because I saw the output.

like image 822
Rollerball Avatar asked Feb 15 '13 16:02

Rollerball


3 Answers

This is because the File represents a abstract path, see the JavaDoc for it http://docs.oracle.com/javase/6/docs/api/java/io/File.html. It does not represent a file handle in the OS.
The line in your code:

PrintWriter pw = new PrintWriter(file);

Simply creates a new file. Try deleting the file after calling this...

like image 180
Boris the Spider Avatar answered Sep 28 '22 12:09

Boris the Spider


File object represents a path to a physical file on the file system either exists or not. That's why you have exists() (to check if it exists) and createNewFile() (to create the file if it is not found).


Also note that PrintWriter(File file) creates a new file if it does not exist.

Parameters:

file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

like image 45
Eng.Fouad Avatar answered Sep 28 '22 13:09

Eng.Fouad


The File is handle to real file (that exists or not). You create and then delete the file above, as you say - all good so far.

When you come to the PrintWriter later on it creates the file once more when you use it - it doesnt matter that you deleted it before.

In fact depending on your use case this might be exaclty wht you want - you may want to delete an old log file for example before re-createing and writing to it once more.

Finally, nothing in your code will be eligible for garbage collection until your method exist, and even then the underyling file will continue to exist (if you dont delete it agin) - and any garbage colleciton in this case wouldnt effect the underlying file. It'll be deleted after the delete invokation and exist again once youre PrintWriter is done with it.

Hope this helps!

like image 24
Sean Landsman Avatar answered Sep 28 '22 12:09

Sean Landsman