1, Use of File class

catalogue

1, Concept

2, Create an instance of a class

(1) Common constructors

1. Constructor 1 (public File(String pathname))

2. Constructor 2 (public File(String parent,String child))

3. Constructor 3 (public File(File parent,String chile))

(2) Path

1. Relative path

2. Absolute path

3. Path separator

   ①. There are different path separators in different systems

   ②. matters needing attention

III. common methods of File class

(1) Get

public String getAbsolutePath()

public String getPath()

public String getName()

public String getParent()

public long length()

public long lastModefied()

 1. Use of the above methods:

2. Create a new file and write its contents

3. Run the above code again

2. The following two methods apply to file directories:

① public String[] list()

② public File[] listFiles()

(2) Rename

public boolean renameTo(File dest)

(2) Judge

public boolean isDirectory()

public boolean isFile()

public boolean exists()

public boolean canRead()

public boolean canWrite()

public boolean isHidden()

(3) Create

public boolean createNewFile(): file creation

public boolean mkddir(): creation of file directory

public boolean mkdirs(): creation of file directory

(IV) deletion

 public boolean delete()

1, Concept

⚪ java.io.File class: abstract representation of file and file directory path, which is platform independent

⚪ File can create, delete and rename files and directories, but file cannot access the file content itself. If you want to access the file content itself, you need to use input \ output stream

⚪ To represent a real File or directory in a Java program, there must be a File object, but a File object in a Java program may not have a real File or directory

⚪ The File object can be passed as a parameter to the stream constructor

2, Create an instance of a class

(1) Common constructors

1. Constructor 1 (public File(String pathname))

    @Test
    public void test1(){
        File file1 = new File("hello.txt");
        File file2 = new File("E:\\BaiduNetdiskDownload\\javasenior\\day08\\src\\java4\\he.txt");
        System.out.println(file1);
        System.out.println(file2);
    }

2. Constructor 2 (public File(String parent,String child))

        File file3 = new File("E:\\BaiduNetdiskDownload","javasenior");

3. Constructor 3 (public File(File parent,String chile))

        File file4 = new File(file3,"hi.txt");

(2) Path

1. Relative path

Compared to the path specified under a path

    @Test
    public void test1(){
        File file = new File("hello.txt");
    }

2. Absolute path

The path to the file or file directory that contains the drive letter

File file2 = new File("E:\\BaiduNetdiskDownload\\javasenior\\day08\\src\\java4\\he.txt")

3. Path separator

Each level of directories in the path is separated by a path separator

   ①. There are different path separators in different systems

  • Windows and DOS systems are represented by "\" by default
  • UNIX and URL s are represented by "/"

   ②. matters needing attention

Java programs support cross platform operation, so the path separator should be used with caution

To solve the above hidden dangers, the File class provides a constant:

public static final String separator

Provide separators dynamically according to the operating system

 

III. common methods of File class

(1) Get

public String getAbsolutePath()

Get absolute path

public String getPath()

Get path

public String getName()

Get name

public String getParent()

Get upper level file directory path

public long length()

Get the file length (i.e. the number of bytes), but cannot get the length of the directory

public long lastModefied()

Gets the last modification time (in milliseconds)

 1. Use of the above methods:

    @Test
    public void test2(){
        File file1 = new File("hello.txt");
        File file2 = new File("E: \\IO\\hi.txt");

        System.out.println(file1.getAbsoluteFile());
        System.out.println(file1.getPath());
        System.out.println(file1.getName());
        System.out.println(file1.getParent());
        System.out.println(file1.length());
        System.out.println(file1.lastModified());
        System.out.println();

        System.out.println(file2.getAbsoluteFile());
        System.out.println(file2.getPath());
        System.out.println(file2.getName());
        System.out.println(file2.getParent());
        System.out.println(file2.length());
        System.out.println(file2.lastModified());
    }

2. Create a new file and write its contents

 

3. Run the above code again

    @Test
    public void test2(){
        File file1 = new File("hello.txt");
        File file2 = new File("E: \\IO\\hi.txt");

        System.out.println(file1.getAbsoluteFile());
        System.out.println(file1.getPath());
        System.out.println(file1.getName());
        System.out.println(file1.getParent());
        System.out.println(file1.length());
        System.out.println(new Date(file1.lastModified()));
        System.out.println();

        System.out.println(file2.getAbsoluteFile());
        System.out.println(file2.getPath());
        System.out.println(file2.getName());
        System.out.println(file2.getParent());
        System.out.println(file2.length());
        System.out.println(file2.lastModified());
    }

2. The following two methods apply to file directories:

① public String[] list()

Gets the name array of all files or file directories in the specified directory

    @Test
    public void test3(){
        File file = new File("E:\\BaiduNetdiskDownload\\javasenior");

        String[] list = file.list();
        for(String s : list){
            System.out.println(s);
        }
    }

 

② public File[] listFiles()

Gets the File array of all files or File directories in the specified directory

        File[] files = file.listFiles();
        for(File f : files){
            System.out.println(f);
        }

(2) Rename

public boolean renameTo(File dest)

Rename the file to the specified file path

In file1 Rename to (File2) as an example:

To ensure that the return is true, file1 must exist in the hard disk, and file2 must not exist in the hard disk

    @Test
    public void test4(){
        File file1 = new File("hello.txt");
        File file2 = new File("E:\\IO\\hi.txt");
        boolean renameTo = file1.renameTo(file2);
        System.out.println(renameTo);
    }

(2) Judge

public boolean isDirectory()

Determine whether it is a file directory

public boolean isFile()

Determine whether it is a file

public boolean exists()

Judge whether it exists

public boolean canRead()

Judge whether it is readable

public boolean canWrite()

Determine whether it is writable

public boolean isHidden()

Determine whether to hide

    @Test
    public void test5(){
        File file1 = new File("hello.txt");

        System.out.println(file1.isFile());
        System.out.println(file1.isDirectory());
        System.out.println(file1.exists());
        System.out.println(file1.canRead());
        System.out.println(file1.canWrite());
        System.out.println(file1.isHidden());
    }

(3) Create

public boolean createNewFile(): file creation

    @Test
    public void test6() throws IOException {
        File file1 = new File("hi.txt");
        if(!file1.exists()){
            file1.createNewFile();
            System.out.println("Created successfully!");
        }else {//File exists
            file1.delete();
            System.out.println("Delete succeeded!");
        }
    }

public boolean mkddir(): creation of file directory

        File file1 = new File("E:\\IO\\io1");
        boolean mkdir = file1.mkdir();
        if(mkdir){
            System.out.println("Created successfully!");
        }

public boolean mkdirs(): creation of file directory

        File file2 = new File("E:\\IO\\io2");
        boolean mkdirs = file2.mkdirs();
        if(mkdirs){
            System.out.println("Created successfully!");
        }

difference:

    @Test
    public void test7(){
        File file1 = new File("E:\\IO\\io1\\io3");
        boolean mkdir = file1.mkdir();
        if(mkdir){
            System.out.println("Created successfully 1!");
        }
        File file2 = new File("E:\\IO\\io1\\io4");
        boolean mkdirs = file2.mkdirs();
        if(mkdirs){
            System.out.println("Successfully created 2!");
        }
    }

 

When io1 does not exist, it can be created successfully by using mkdirs()

(IV) deletion

 public boolean delete()

 

Keywords: Java Back-end Network Protocol p2p

Added by supratwinturbo on Wed, 19 Jan 2022 08:47:08 +0200