I/O -- File object

File file object

File s and folders are represented by files

Create a file object, (no real files or folders will be created)

File f1 = new File("d:/lolfilder");
        System.out.println("f1 Absolute path of:"+f1.getAbsolutePath());
        File f2 = new File("lol.exe");
        System.out.println("f2 Absolute path of:"+f2.getAbsolutePath());
        File f3 = new File(f1, "lol.exe");
        System.out.println("f3 Absolute path of:"+f3.getAbsolutePath());
        //mkdir()Create folder failed if parent folder does not exist
        //f3.mkdir();
        //mkdirs()Create folder if the parent folder does not exist, create the parent folder and then create the folder again
        f3.mkdirs();
        System.out.println("f3 Does it exist"+f3.exists());
        //list()Returns a string array package containing all the file names under the current folder
        String[] fStrings=f1.list();
        for(String s:fStrings) {
            System.out.println(s);
            
        }
        

Traversal folder exercise

Traverse all the files in the C:\WINDOWS directory (without traversing subdirectories). Find out the largest and smallest (non-zero) files in these files, and print out their file names

Requirement: minimum file cannot be 0 length

Implementation idea: it's simpler not to traverse subdirectories, establish the maximum file and the minimum file objects, and then use loops to ensure the maximum and minimum. Note that the minimum file cannot be 0, so give min a value, otherwise the file with size 0 is smaller than all files

public class test {
/**
 * 
 * Traverse all files (excluding those under subdirectories) under the windows folder of Disk c, and list the largest and smallest
 */
    public static void main(String[] args) {
        File file = new File("C:\\Windows");
        File max=new File(" ");
        File min = new File(" ");
        //use listFiles Method to get a File Array of type
        File []fs = file.listFiles();
        //foreach
        for(File f:fs) {
            
        //to min An initial value, or it will always be the smallest
        if(min.length()==0)
             min=max;
        //than max Big, replace max    
        if(max.length()<f.length()) {
            max= f;
        }
        
        //than min Small substitution min
        if(min.length()>f.length()&&f.length()!=0) {
            min= f;
        }
        
            System.out.println("The file name is:"+f.getAbsolutePath()+"The size is:"+f.length());
        }
        
        System.out.println("The maximum file size is:"+max.getAbsolutePath()+"The size is:"+max.length());
        System.out.println("The minimum file is:"+min.getAbsolutePath()+"The size is:"+min.length());
        

    }

}

The next step is to traverse the subdirectories:

The idea is to use recursion. If its current file is a folder, continue to call this method

public class List {
    /**
     * 
     * Use recursion to traverse all files (including those under subdirectories) under the windows folder of c disk, and list the maximum and minimum
     */
    
    File max = new File(" ");
    File min = new File(" ");
    
    
    public void listFile(File file) {
        
            //If f Is a folder, start recursion
            if(file.isDirectory()) {
                 File[] fs = file.listFiles();
                    if(null!=fs)
                    for (File f : fs) {
                        listFile(f);
                    }
            }
            //If file It's a document.
            if(file.isFile()) {
            
            
            //to min An initial value, or it will always be the smallest
            if(min.length()==0)
                 min=max;
            //than max Big, replace max    
            if(max.length()<file.length()) {
                max= file;
            }
            
            //than min Small substitution min
            if(min.length()>file.length()&&file.length()!=0) {
                min= file;
            }

            
            return;
        }
        }
        
    

}

Keywords: Java Windows

Added by Megienos on Sun, 17 Nov 2019 21:57:25 +0200