day18: File (construction method, creating and deleting files or folders, judgment, renaming and cutting, availability, filtering), recursion (traversing folder files)

Posted by Ryan Sanders on Sat, 05 Mar 2022 02:13:27 +0100

I. review

1.HashMap set

Features:

A. the data structure is also a Hash table structure

B. It is unsafe in multithreading

C. the initialization capacity of the default array is 16

2. Comparison between HashMap and HashSet

The same point: they are stored in hash table structure

Differences:

The bottom layer of A.HashSet is implemented using HashMap

B. the data structure of HashSet is for the data structure of element {HashMap, which is for key 3 LinkedHashMap    

Implementation of hash table and link list of A.Map interface

B. predictable iteration sequence

C. This implementation is not unsafe in synchronous multithreading

4. Generics:

Generic usage scenario: you can use generic when you are uncertain about the data type when defining a collection. Generic is a special variable used for the receiver data type

5. Use generics

List < data type > object name = new ArrayList < data type > ();      

Attention points;        

A. the generics before and after must be consistent

B. in jdk1 After the diamond generic 7 can be omitted

C. generics can only be reference data types, not basic data types

6. Define generic classes:

Syntax:

Access modifier class class name < generic > {

All members can use their data types

                        }

7. Method of defining generics:

Syntax:

Access decorator < generic > return value type method name (parameter list){

Method body

Return return value

                }        

Note:

A. common member methods are generic types that can use classes

B. static methods are generic types that cannot use classes

8. Define generic interfaces

A. the implementation class determines its genericity

B. the implementation class is uncertain about its genericity

9. Generic wildcards? It is generally used as a parameter of the method

        <? Extensions E > = = > e itself or its subclass

        <? Super E > = = > e itself or its parent class

10. Nested traversal of map set 11 Landlords case

6, file

6.1 introduction

1. Abstract representation of File and directory pathnames = = > the File or File path can be formed into a File, and its method can be used

2. Road strength: absolute road strength and relative path

3. Absolute road force: the path starting from the root drive letter is the absolute road force. Example: D: \ day02 \ SRC \ test01 java

4. Relative path: the path relative to the specific reference object is basically the relative path used in idea

Relative path

6.2 construction method of file

Name of construction methodDescription of construction method
public File(File parent, Stringchild)Create a new File instance based on the parent abstract pathname and the child pathname string
public File(String pathname)Create a new File instance by converting the given pathname string to an abstract pathname
public File(String parent, Stringchild)Create a new File instance based on the parent pathname string and the child pathname string

code

public class Test01 {
    public static void main(String[] args) throws IOException {
        File f = new File(new File("c:\\admin"), "1.txt");
        File f1 = new File("c:\\admin","2.txt");
        File f2 = new File("c:\\admin\\ad\\3.txt");
    }
}

6.3 method of creating

Name of the methodDescription of method
public boolean createNewFile()create a file
public boolean mkdir()Create folder (only one level directory can be created)
public boolean mkdirs()Create folder (create multi-level directory)

//Generally, you need a directory to create files
//Files and directories cannot be created at the same time
 //1. You can create directories before creating files

code

 public static void main(String[] args) throws IOException {
        //Generally, you need a directory to create files
        //Files and directories cannot be created at the same time
        //1. You can create a directory first and then a file
        //File file = new File("d:\\admin\\a");
        //This is to create a level-1 directory. If a multi-level directory is filled in, the creation fails by default
        File file = new File("d:\\admin");
        file.mkdir();
        //There are already admin s available
        File file1 = new File("d:\\admin\\a\\b");
        file1.mkdirs();

    }

6.4 deleting files or folders

Name of the methodDescription of method
public boolean delete()Delete files and empty folders (cannot be multi-level directories)

Note: files or empty folders that use delete will not enter the recycle bin

    //Delete file
        File file1 = new File("d:\\admin\\a\\b\\1.txt");
        //Delete 1 txt
        file1.delete();
        //Delete empty folder
        File file2= new File("d:\\admin\\a\\b");
        //Delete b folder
        file2.delete();
        File file3= new File("d:\\admin");
        //Deleting a multi-level empty folder with files in it fails
        file3.delete();

6.5 cases

step01 requirements

step02 analysis

1. Create a multi-level directory folder (mkdirs)

2. Create a file (createnewfile)

//Generally, you need a directory to create files
        //Files and directories cannot be created at the same time
        //1. You can create a directory first and then a file
        //File file = new File("d:\\admin\\a");
        //This is to create a level-1 directory. If a multi-level directory is filled in, the creation fails by default
        File file = new File("d:\\admin");
        file.mkdir();
        //There are already admin s available
        File file1 = new File("d:\\admin\\a\\b");
        file1.mkdirs();

6.6 judgment

Name of the methodDescription of method
public boolean exists()Test whether the file or directory represented by this abstract pathname exists
public boolean isDirectory()Test whether the file represented by this abstract pathname is a directory
public boolean isFile()Determine whether it is a file

7.6 cutting and renaming

Name of the methodDescription of method
public boolean renameTo(Filedest)Rename (rename in the same directory) or cut (cut in different directories, and the original directory will no longer exist)
//The file you want to file first does not exist
File file1 = new File("d:\\a\\b\\c\\d.txt");
File file2 = new File("d:\\a\\b\\c\\d.pdf");
file1.renameTo(file2);

6.8 method of obtaining

    File f1 = new File("d:\\a\\b\\c\\d.txt");
        String absolutePath = f1.getAbsolutePath();
        System.out.println(absolutePath);
        System.out.println(f1.getPath());
        System.out.println(f1.getName());
        System.out.println(f1.getParent());
        System.out.println(f1.length());
d:\a\b\c\d.txt
d:\a\b\c\d.txt
d.txt
d:\a\b\c
0
File f1 = new File("d:\\a\\b\\c");
System.out.println(f1.getName());

c

6.9 filterability

Code - 01

 public static void main(String[] args) {
        //File f1 = new File("d:\\a\\b\\c");
        File file = new File("D:\\admin");
        String[] list = file.list();
        for (String s : list) {
            System.out.println(s);
        }
    }
a

Code - 03

6.9.1 cases

step01 requirements

step03 code

 7. recursion

7.1 introduction

1. Recursive transfer, return and unification in development, recursion means that the method calls itself and develops towards the non recursive method, so as to gradually change the range of size into a small range

2. Recursion: the method calls itself

3. Note: A. recursion is developing in the direction of non recursion # method parameters will gradually decrease # B. recursion is prone to stack memory overflow

7.2 recursion in Mathematics

Recursion from large to small

public static void main(String[] args) {

    }
    public static int showNum(int num){
        if (num<1){
            return -1;
        }else if (num==1){
            return 1;
        }else {
            return num*showNum(num-1);
        }

    }

Recursion from small to large

public static void main(String[] args) {
        System.out.println(showNum(10));
    }
    public static int showNum(int num){
        if (num==1){
            return 1;
        }else if (num==2){
            return 1;
        }else {
            return showNum(num-1)+showNum(num-2);
        }

    }

7.3 recursive memory diagram

 

7.4 cases

step01 requirement: delete all files under a folder, including itself, in a recursive way

step02 analysis

A. Parameter File object File of method in recursion

B.istFile() gets all files and directories in the current directory

C. Use the loop to traverse the array to determine whether the directory continues recursion rather than directory deletion ()

7.5 cases

step01

Print all the pictures in Disk c under the console and use step02 analysis in a recursive way

1. Operate with recursion

2. Recursion is required when it is a directory

3. Filter out all pictures and judge their suffixes

step03 code

Topics: data structure JavaSE p2p