Java Files  «Prev 


Prior to NIO2

Deleting Java File

The delete() method removes files from the filesystem permanently:
public boolean delete()

This method returns true if the file existed and was deleted. (You can't delete a file that does not exist.)
If the security manager disallows this action, a security exception is thrown. Otherwise, delete() returns false. You can move a file by copying it then deleting the original. For example, to move the file src to the file dst:

FileInputStream fin = new FileInputStream(src);
FileOutputStream fout = new FileOutputStream(dst);
StreamCopier.copy(fin, fout);
fout.close();
fin.close();
src.delete();

Java I/O
The warnings about copying files (meta-information and resource forks not maintained, copies file-to-file only supported) go double for this sort of move, because after the move is complete, the original file and all its data and meta-information are deleted. Any information that was not copied is lost.
The method f.delete() tries to delete the file f. This method returns true if the file existed and was deleted.
(You can not delete a file that does not exist). Otherwise it returns false.
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.
Returns true if and only if the file or directory is successfully deleted; false otherwise