File class delete function operation example

039545775f23ceb8a33b77979c0abe9d.png

public  class FileDemo04 {
     public  static  void main (String [] args) throws IOException {
         // Requirement 1: Create a java.txt file under the current module directory 
        File f1 = new File ("myFile \\ java.txt" );
 //         System.out.println (f1.createNewFile ()); 

        // Requirement 2: Delete the java.txt file in the current module directory 
        System.out.println (f1.delete ()); 
        System.out.println ( "--- ----- " ); 

        // Requirement 3: Create an itcast directory under the current module directory 
        File f2 = new File (" myFile \\ itcast " ); 
        System.out.println (f2.mkdir ()); 

        //Requirement 4: Delete the itcast directory under the current module directory 
        System.out.println (f2.delete ()); 
        System.out.println ( "--------" ); 

        // Requirement 5: In the current module Create an itcast directory, and then create a file java.txt 
        File f3 = new File ("myFile \\ itcast" ); 
        System.out.println (f3.mkdir ()); 
        File f4 = new File (" myFile \\ itcast \\ java.txt " ); 
        System.out.println (f4.createNewFile ()); 

        // Requirement 6: Delete the itcast directory under the current module 
        / * 
            When there is content in the directory (directory, file) , Can not be deleted directly 
         * / 
        System.out.println (f4.delete ()); // Delete the contents of the directory first 
        System.out.println (f3.delete ()); //Direct deletion will return false 
    } 
}

Points to note when deleting directories:

If there is content (directory, file) in a directory, it cannot be deleted directly. The contents of the directory should be deleted before the directory can be deleted last

Guess you like

Origin www.cnblogs.com/pxy-1999/p/12697275.html