Explanation of File class and use of common methods

File class

Concept: The File class represents a specific file or directory (folder) and provides several methods to operate on the file or directory.

File class construction method
There are three commonly used methods:
1.File(String pathname): Create a new File instance through the path name.

2.File(File parent, String child): Create a new File instance based on the parent directory path File instance and the subdirectory or file path.

3.File(String parent, String child): Create a new File instance based on the parent directory path and the subdirectory or file path.

Commonly used methods of File class
There are roughly the following types:

isDirectory() Is it a folder
isFile() Is it a file
getPath() Gets the path of file
getName() Get the name of the last layer
getParent() Get the path without the last layer
getParentFile() Get the new file with the parent class path< /span> separator replaces slashes or backslashes in file or folder paths to prevent cross-platform errors listFiles() Returns the name of the files under the path File array composed of files or folders list() Returns the name array of files or folders under the path delete( ) Delete files or directories (empty directories) exists() whether the path exists createNewFile() can create a new file, only one level mkdirs() create a new folder , can have multiple levels mkdir() create a new folder, only one layer can be created
renameTo() rename







File file class

Instances of the File class are immutable; that is, once the object is created, the abstract pathname represented by the File object never changes.

1. isFile(): Determines whether the specified object is a file and returns a boolean value.

2. isDirectory(): Determines whether the specified object is a directory and returns a boolean value.
Note: The usage method is the same as isFile(), so I won’t give a case~~ There is also a similar method isHidden(): determine the current Whether the object is a hidden file or directory.

3. getName(): Get the name of the file or directory.

4. getPath(): Get the relative path of the specified object.
Note: This method is similar to getName(). If you remember the usage of one, you can remember them together. Another one that is similar is getParent(): Gets the path of the parent directory of the specified object, and returns null if there is no parent directory.

5. canRead(): Determine whether the current object is readable and return a boolean value
Note:canWrite(): Determine whether Writable, consistent with the canRead() method.

6, mkdir(): Create a single-level directory
Supplement:mkdirs(): Create a multi-level directory. mkdir() can only create one level of directories downwards, but this can create parent directories together.

10. delete(): delete the file. Note that it is permanently deleted and cannot be restored. When deleting a directory, only empty directories can be deleted.

public class Demo07 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("D:/aa/bb/cc");
        f1.mkdirs();
        File f2 = new File(f1,"c.text");
        f2.createNewFile();


    }
}

Guess you like

Origin blog.csdn.net/weixin_69036336/article/details/128225521