2008-11-21 25 views

Respuesta

4

¿No da como resultado una excepción FileNotFoundException?

EDIT:

hecho que da lugar a falsas:

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

impresiones false

1

el Javadoc oficial:

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 

así, falsa.

8

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

Devuelve: verdadero si y sólo si el archivo o directorio está eliminado con éxito; falso de lo contrario

Por lo tanto, debe devolver falso para un archivo inexistente. La siguiente prueba confirma esto:

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

Al compilar y ejecutar este código se obtiene un resultado falso.

Cuestiones relacionadas