Learning notes: File class

Posted by xeel on Wed, 01 Dec 2021 08:51:17 +0100

Learning content: File class

1. Overview of the File class

1.1. File class has nothing to do with the "four families". File is only an abstract representation of path name, so file class cannot read and write files.
1.2. File object is an abstract representation of file and folder (directory) path names. A file object corresponds to a directory or a folder (directory).

2. Method of File class

2.1 construction method
File(String pathname) creates a new file instance by converting the given pathname string to an abstract pathname.
File(String parent,String child) creates a new file object instance according to the specified parent path and file path
File(File parent,String child) creates a new file object instance according to the specified parent path object and file path

2.2 common methods
boolean exists() determines whether there is a

boolean createNewFile() specifies the path to create a file when the file does not exist, and returns true, otherwise false
boolean mkdir() creates a folder (directory) when the specified click folder (directory) does not exist and returns true; otherwise, false
boolean mkdirs(), but if the specified multi-level folder (directory) does not exist, create a multi-level folder (directory) and return true; otherwise, false
boolean delete() deletes files or single level folders. Delete the folder. There can be no other files or folders under this folder
Code example:

import java.io.File;

public class Test01 {
    public static void main(String[] args) throws Exception {
        //Create a File object
        File f1=new File("D:\\file");

        //Judge whether it exists
        System.out.println(f1.exists());

        //If D:\file does not exist, it is created as a file
        if(!f1.exists()){
            //As before, the exception is thrown directly because of trouble here
            f1.createNewFile();
        }

        //If D:\file does not exist, it is created as a file
        if(!f1.exists()){
            //New as directory
            f1.mkdir();
        }

        //Create multiple directories
        File f2=new File("D:\\a\\b\\c\\d");
        if (!f2.exists()) {
           //Multi directory form new
            f2.mkdirs();
        }
        
		//Delete file
        System.out.println(f1.delete());
    }
}

String getParent() gets the parent path of the current path and returns the parent path as a string
String getAbsolutePath() gets the absolute path of the file and returns the string of the path
File getAbsoluteFile() gets the absolute path of the file and returns the file object

File f3=new File("D:\\Altman collection\\Diga Altman\\Test.txt");
//Gets the parent path of the file
String parentPath=f3.getParent();
System.out.println(parentPath);//D: \ Altman collection \ diga Altman

File parentFile=f3.getParentFile();
System.out.println("Get absolute path:"+parentFile.getAbsoluteFile());//Get absolute path: D: \ Altman collection \ diga Altman

//Get absolute path
File f4=new File("copy");
System.out.println(f4.getAbsoluteFile());//D:\IDEA\Project\Serein\copy

File f5=new File("myfile");
System.out.println(f5.getAbsolutePath());//D:\IDEA\Project\Serein\myfile

String getName() gets the name of the file or folder
boolean isDirectory() determines whether it is a directory
boolean isFile() determines whether it is a file
long lastModified() returns the last modified time in milliseconds
long length() gets the file size (bytes)
boolean renameTo(File dest) rename

File f1=new File("D:\\Altman collection\\Diga Altman\\Diga Altman Episode 1.txt");
//String getName() gets the name of the file or directory
System.out.println("File name:"+f1.getName());//File name: diga Altman episode 1.txt

//boolean isDirectory() determines whether it is a directory
System.out.println(f1.isDirectory());//false

//boolean isFile() determines whether it is a file
System.out.println(f1.isFile());//true

long haoMiao=f1.lastModified();//This is the total number of milliseconds since 1970
//Convert total milliseconds to date
Date time=new Date(haoMiao);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String strTime=sdf.format(time);
System.out.println(strTime);//2021-12-01 09:43:54 278

//Get file size
System.out.println(f1.length());//842 bytes

//rename
File file = new File("a.txt");
File file1 = new File("e.txt");
System.out.println(file.renameTo(file1));

File[] listFile returns all file and folder names under the current path in the form of file object

        //Get all the sub files in the current directory
        File f=new File("D:\\JDK");
        File[] files=f.listFiles();
        //foreach
        for (File file:files){
            System.out.println(file.getAbsolutePath());//Get absolute path
            System.out.println(file.getName());//Get file name
        }

Topics: Java Algorithm data structure