2008-11-21 9 views

Répondre

4

Cela ne résulte-t-il pas en une exception FileNotFoundException?

EDIT:

En effet, il ne résulte en faux:

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 + "|"); 
    } 
} 

impressions false

1

Le javadoc officiel:

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 
Throws: 
    SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file 

donc, faux.

8

De http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#delete():

Retourne: true si et seulement si le fichier ou le répertoire est supprimé avec succès; false sinon

Par conséquent, il doit renvoyer false pour un fichier inexistant. Le test suivant confirme ceci:

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

La compilation et l'exécution de ce code génèrent false.

Questions connexes