JavaIO stream -- use of File class

File class overview

File class can create, delete and rename files and directories, but cannot access the file content itself. If you need to access the file content itself, you need to use the subsequent 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 constructor of the stream.

Common constructors

①public File(String pathname)
Create a File object with pathname as the path, which can be an absolute path or a relative path. If it is a relative path, it is relative to the current module by default.

File file1 = new File("hello.txt"); //Relative path
File file2 = new File("C:\\IDEA\\untitled\\file\\hi.txt"); //Absolute path

②public File(String parent,String child)
Create a File object with parent as the parent path and child as the child path.

File file3 = new File("C:\\IDEA\\untitled\\", "file");

③public File(File parent,String child)
Creates a File object based on a parent File object and a child File path

File file3 = new File("C:\\IDEA\\untitled\\", "file");
File file4 = new File(file3, "hi.txt");

common method

① Get function of File class
public String getAbsolutePath(): get absolute path
public String getPath(): get path
public String getName(): get the name
public String getParent(): get the directory path of the upper level file. If not, return null
public long length(): get the length of the file (i.e. the number of bytes), but not the length of the directory
public long lastModified(): gets the last modified time, in milliseconds
public String[] list(): gets the name array of all files or file directories in the specified directory
public File[] listFiles(): gets an array of all files or file directories in the specified directory

import java.io.File;

/**
 * @Author: Yeman
 * @Date: 2021-09-24-21:50
 * @Description:
 */
public class FileTest {
    public static void main(String[] args) {
        File file1 = new File("hello.txt");
        File file2 = new File("C:\\IDEA\\untitled\\file\\hi.txt");

        System.out.println(file1.getAbsolutePath());
        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(file2.getAbsolutePath());
        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());
    }
}

② Rename function of File class
public boolean renameTo(File dest): Renaming the file to the specified file path and file name is equivalent to transferring and renaming the real file

import java.io.File;

/**
 * @Author: Yeman
 * @Date: 2021-09-24-21:50
 * @Description:
 */
public class FileTest {
    public static void main(String[] args) {
        File file1 = new File("hello.txt"); //file1 needs to exist in the hard disk
        File file2 = new File("C:\\IDEA\\hi.txt"); //file2 does not exist on the hard disk
        boolean b = file1.renameTo(file2);
        System.out.println(b);
    }
}

③ Judgment function of File class
The hard disk must be real in order to make a real judgment
public boolean isDirectory(): judge whether it is a file directory
public boolean isFile(): judge whether it is a file
public boolean exists(): judge whether it exists
public boolean canRead(): judge whether it is readable
public boolean canWrite(): judge whether it is writable
public boolean isHidden(): judge whether to hide

④ File class creation function
public boolean createNewFile(): creates a file. If the file exists, it will not be created. false is returned
public boolean mkdir(): create a file directory. If the file directory exists, it will not be created. If the upper directory of the file directory does not exist, it will not be created
public boolean mkdirs(): create a file directory. If the upper file directory does not exist, create it together

⑤ Delete function of File class
public boolean delete(): deletes files or folders
Delete precautions: delete in Java does not go to the recycle bin. To delete a file directory, please note that the file directory cannot contain files or file directories

import java.io.File;
import java.io.IOException;

/**
 * @Author: Yeman
 * @Date: 2021-09-24-21:50
 * @Description:
 */
public class FileTest {
    public static void main(String[] args) throws IOException {
        File file1 = new File("hello.txt");
        if (!file1.exists()){
        	//File does not exist, create
            file1.createNewFile();
            System.out.println("Created successfully");
        }else {
        	//File exists, delete
            file1.delete();
            System.out.println("Delete succeeded");
        }
    }
}

Tags: Java Class

Posted on Fri, 24 Sep 2021 11:08:00 -0400 by mlampard