2019-06-02 Java recursive learning exercise diary day23

Recursive practice

Exercise 1

Import java.io.File;
 Import java.util.Scanner; 

public  class test1 { 

    public  static  void main (String [] args) {
         // File GetDir the dir = ();
         // System.out.println (getFileLength (the dir) ); 
        File the dir = new new File ( "D: \\ \\ Java 2018" ); 
        System.out.println (dir.length ()); 

    } 
    
    public  static File GetDir () { 
        Scanner SC = new new Scanner (the System.in ); 
        System.out.println ( "Please enter a folder path" );
         // define an infinite loop 
        while( To true ) { 
            String Line = sc.nextLine (); 
            File dir = new new File (Line); 
            
            // for a File object to determine 
            IF (! Dir.exists ()) { 
                System.out.println ( "You entered folder path does not exist, enter a folder path: " ); 
            } the else  IF (dir.isFile ()) { 
                System.out.println ( " enter your file path, enter a folder path: " ); 
            } the else {
                 // the packet plus the return path of the object 
                return the dir; 
            } 
        } 
    } 
    public  static Long getFileLength (File the dir) {    
         // . 1, the definition of a variable summing 
        Long len = 0 ;
         // 2, obtaining listFiles folder of the folder and all the files and folders (); 
        File [] = subFiles dir.listFiles () ;            
         // 3, through the array 
        for (file SUBFILE: subFiles) {
             // . 4, calculates the size of the file is determined and accumulated 
            IF (subFile.isFile ()) { 
                len = len + subFile.length ();
             // . 5, Analyzing the folder, recursive calls 
            } the else { 
                len = len + getFileLength (SUBFILE); 
            } 
        }
        return len; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/JungTan0113/p/10965453.html