IO stream (day 23 of learning Java) (File class)

Posted by brendandonhue on Mon, 21 Feb 2022 13:55:46 +0100

catalogue

I File class overview

II Static member variable of File class

III Construction method of File class

III Get method of File class

IV Create and delete functions of File class

Vi File class directory traversal method

VII File filter of file class

I File class overview

java. io. Abstract representation of file and directory pathnames; It is mainly used for creating, searching and deleting files and directories. File class encapsulates files, folders and paths into objects and provides a large number of methods to operate these objects.

  • File: container for storing actual data, file
  • Directory: folder, directory
  • Path: the storage location of the file or directory on the disk, path

II Static member variable of File class

  • static String pathSeparator: path separator related to the system
    • Window operating system, the separator is a semicolon
    • Linux operating system, the separator is colon
  • static String separator default name separator associated with the system
    • Window operating system, the name division symbol is\
    • Linux operating system, name separator is/

public class Demo01 {
    public static void main(String[] args) {
        //static String pathSeparator
        //The path separator associated with the system. For convenience, it is represented as a string.
        String pathSeparator = File.pathSeparator;
        System.out.println(pathSeparator);

        //static String separator
        // The default name separator associated with the system, which is represented as a string for convenience.
        String separator = File.separator;
        System.out.println(separator);
    }
}

 

 

III Construction method of File class

  • 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 instance based on the parent pathname string and the child pathname string
  • File(File parent, String child) creates a new file instance based on the parent abstract pathname and the child pathname string
public class Demo02 {
    public static void main(String[] args) {
        method01();
        method02();
        method03();
    }

    //File(String pathname)
    //Create a new File instance by converting the given pathname string to an abstract pathname.
    public static void method01(){
        File file = new File("D:\\ORACLE\\a");
        System.out.println(file);
    }

    // File(String parent, String child) creates a new file instance based on the parent pathname string and the child pathname string
    // The parent path and the child path of the transfer string can operate the parent path and the character separately
    public static void method02(){
        File file = new File("D:\\ORACLE", "a\\a.java");
        System.out.println(file);
    }

    //File(File parent, String child) creates a new file instance based on the parent abstract pathname and the child pathname string
    // Pass the parent path of File type and the child path of string
    public static void method03(){
        File parent = new File("D:\\ORACLE");
        File file = new File(parent, "a.java");
        System.out.println(file);
    }
}

 

III Get method of File class

  • String getName() returns the name of the file or directory represented by this abstract pathname
  • File getParentFile() returns the abstract pathname of the parent directory of this abstract pathname; If no parent directory name is specified, null is returned
  • long length() returns the length of the file represented by this abstract pathname
  • File getAbsoluteFile() returns the absolute pathname form of this abstract pathname
  • String getPath() converts this abstract pathname to a pathname string
public class Demo03 {
    public static void main(String[] args) {
//        method01();
//        method02();
//        method03();
//        method04();
        method05();
    }
    /**
     * String getName() Returns the name of the file or directory represented by this abstract pathname
     *        Gets the last name of the path encapsulated in the constructor
     *        The name may be a folder name or a file name
     */
    public static void method01(){
        File file = new File("D:\\ORACLE\\a");
        System.out.println(file.getName());

        File file1 = new File("D:\\ORACLE\\a\\A.java");
        System.out.println(file1.getName());
    }

    /**
     * File getParentFile() Returns the abstract pathname of the parent directory of this abstract pathname; Returns null if the pathname does not specify a parent directory
     *         Gets the parent path of the path encapsulated in the constructor
     *         Method returns a File object. You can use the method call chain
     */
    public static void method02(){
        File file = new File("D:\\ORACLE\\a");
        File parentFile = file.getParentFile();
        System.out.println(parentFile);
        System.out.println(parentFile.getParentFile());
    }

    /**
     * long length() Returns the length of the [file] represented by this abstract pathname
     *         Gets the number of bytes of the file represented by the path in the construction method
     */
    public static void method03(){
        File file = new File("D:\\ORACLE\\a\\c.txt");
        System.out.println(file.length());

        File file1 = new File("D:\\ORACLE\\a");
        System.out.println(file1.length());
    }

    /**
     *  File getAbsoluteFile() Returns the absolute pathname form of this abstract pathname
     *         Returns the absolute path of the path in the construction method. The return value is the File object
     *         Note: the absolute path obtained directly from the file name or folder name of the enemy will be calculated from the project of IDEA
     */
    public static void method04(){
        File file = new File("D:\\ORACLE\\a\\A.java");
        System.out.println(file.getAbsoluteFile());

        File file1 = new File("A.java");
        System.out.println(file1.getAbsoluteFile());
    }

    /**
     * String getPath() Convert this abstract pathname to a pathname string
     *         Converts the path in the constructor to a string type
     */
    public static void method05(){
        File file = new File("D:\\ORACLE\\a\\A.java");
        System.out.println(file.getPath());
    }
}

 

IV Create and delete functions of File class

  • boolean createNewFile() creates a new empty file inseparably if and only if there is no file with the name specified by this abstract pathname
  • boolean mkdirs() creates the directory specified by this abstract pathname, including all required but nonexistent parent directories
  • boolean delete() deletes the file or directory represented by this abstract pathname
public class Demo04 {
    public static void main(String[] args) throws IOException {
//        method01();
//        method02();
        method03();
    }


    /**
     *    boolean createNewFile() If and only if there is no file with the name specified by this abstract pathname, a new empty file is created inseparably.
     *         If a new file does not exist before it is created, true will be returned if it is created successfully
     *         If it does not exist, false is returned
     *         The path to create the File is written in the constructor of the File class
     *         This method can only create files
     */
    public static void method01() throws IOException {
        File file = new File("D:\\ORACLE\\a\\C.java");
        boolean newFile = file.createNewFile();
        System.out.println(newFile);
    }

    /**
     *  boolean mkdirs() Create the directory specified by this abstract pathname, including all required but nonexistent parent directories
     *         If you create a folder that does not exist before, true will be returned if it is created successfully
     *         If the folder exists and is not created, false is returned
     *         This method can only create folders
     *         mkdirs():You can create multi-level directories at the same time
     *         mkdir():Only one level of directory can be created
     */
    public static void method02(){
        File file = new File("D:\\ORACLE\\a\\b\\c");
        boolean mkdirs = file.mkdirs();
        System.out.println(mkdirs);
    }

    /**
     * boolean delete() Delete the file or directory represented by this abstract pathname
     *         Delete directories or files
     *         If the deletion is successful, return true and delete the File constructor encapsulation path
     *         Note: when deleting a folder, ensure that the folder is empty, otherwise it cannot be deleted
     */
    public static void method03(){
//        File file = new File("D:\\ORACLE\\a\\b\\c");
//        boolean delete = file.delete();
//        System.out.println(delete);

        File file1 = new File("D:\\ORACLE\\a\\b");
        file1.delete();
    }
}

 

V File class judgment method

  • boolean exists() tests whether the file or directory represented by this abstract pathname exists
  • boolean isDirectory() tests whether the file represented by this abstract pathname is a directory
  • boolean isFile() tests whether the file represented by this abstract pathname is a standard file
public class Demo {
    public static void main(String[] args) throws Exception {
        method03();
    }

    /*

        boolean exists() Test whether the file or directory represented by this abstract pathname exists

        Judge whether the encapsulated path in the File construction method exists, and return true if it exists

        Does the File or directory represented by this File actually exist
     */
    public static void method01() throws Exception {
        File file01=new File("d:\\a\\b");
        boolean b1 = file01.exists();
        System.out.println(b1);//true
        File file02=new File("d:\\a\\b\\a.txt");
        boolean b2 = file02.exists();
        System.out.println(b2);//true
        File file03=new File("d:\\e.txt");
        boolean b3 = file03.exists();
        System.out.println(b3);//false
    }
    /*

        boolean isDirectory() Test whether the file represented by this abstract pathname is a directory
        Judge whether the path in the File construction method is a folder; If it is a folder, return true
     */
    public static void method02(){
        File file01=new File("d:\\a");
        boolean b1 = file01.isDirectory();
        System.out.println(b1);
    }

    /*
        boolean isFile() Test whether the file represented by this abstract pathname is a standard file
        Judge whether the encapsulated path in the File construction method is a File or not, and return true
     */

    public static void method03(){
        File file01=new File("d:\\a\\b\\a.txt");
        boolean b1 = file01.isFile();
        System.out.println(b1);
    }
}

 

 

Vi File class directory traversal method

  • File[] listFiles() returns an array of abstract pathnames that represent files in the directory represented by this abstract pathname
public class Demo05 {
    public static void main(String[] args) {
        method01();
    }
    public static void method01(){
        File file = new File("D:\\ORACLE\\a");
        File[] files = file.listFiles();
        for (File f : files) {
            System.out.println(f);
        }
        System.out.println(files.length);
    }
}

 

VII File filter of file class

  • Public File [] listfiles (filefilter) returns a File array that represents all sub files or directories in the File directory. Filter is a File filter that can filter unwanted files
  • FileFilter interface
    • File filter interface. The implementation class of this interface can be passed to the method listFiles() to realize the file filtering function
    • FileFilter interface method: public boolean accept(File path): the method parameter is the path of the file or directory obtained by listFiles() method. If the method returns true, it indicates that this path is required, otherwise it will be ignored
public class Demo05 {
    public static void main(String[] args) {
        method01();
    }
    public static void method01(){
        File file = new File("D:\\ORACLE\\a");
        File[] files = file.listFiles(new MyFilter());
        for (File f : files) {
            System.out.println(f);
        }
        System.out.println(files.length);
    }
}
class MyFilter implements FileFilter{
    @Override
    public boolean accept(File pathname) {
        return pathname.getName().toLowerCase().endsWith(".java");
    }
}

 

Topics: Java