In java, does file.delete()
return true
or false
where File file
refers to a non-existent file?
I realize this is kind of a basic question, and easy to very through test, but I'm getting strange results and would appreciate confirmation.
From http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#delete():
Returns: true if and only if the file or directory is successfully deleted; false otherwise
Therefore, it should return false for a non-existent file. The following test confirms this:
import java.io.File;
public class FileTest
{
public static void main(String[] args)
{
File file = new File("non-existent file");
boolean result = file.delete();
System.out.println(result);
}
}
Compiling and running this code yields false.
Doesn't that result in a FileNotFoundException?
EDIT:
Indeed it does result in false:
import java.io.File;
public class FileDoesNotExistTest {
public static void main( String[] args ) {
final boolean result = new File( "test" ).delete();
System.out.println( "result: |" + result + "|" );
}
}
prints false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With