A brief introduction to the File class in java

File class

java.io.FileClass
abstract representation of file and directory path names.

Java encapsulates the files and folders (directories) in the computer into a File class, we can use the File class to operate files and folders

The File class is a system-independent class, and any operating system can use the methods in this class

file: file
directory: folder/directory
path: path

static String pathSeparatorThe path separator related to the system. For convenience, it is represented as a string. That is, "" + pathSeparatorChar
static char pathSeparatorCharsystem-related path separator.

Path separator windows: semicolon; linux: colon:

static String separatorThe default name separator associated with the system. For convenience, it is represented as a string.
static char separatorCharThe default name separator related to the system.

操作路径:
"C:\develop\a\a.txt " windows
"C:/develop/a/a.txt " linux
All system writing
“C:”+File.separator+“develop”+File.separator+“a”+File.separator+“a.txt”

Path separator windows: semicolon; linux: colon:

File name separator windows: backslash\ linux: forward slash/

path:

Absolute path: it is a full path
with a drive letter (C:, D :) beginning of the path
C: \ a.txt
C: \ the Users \ Ren \ 123.txt
D: \ Demo \ b.txt relative path: is a simplified path relative refers

Relative toOf the current projectRoot directory(C:\Users\Ren)
​ If you use the root directory of the current project, the path can be simplified to write
​ C:\Users\Ren\123.txt --> simplified to: 123.txt (the root directory of the project can be omitted)

note:

  1. Path is not case sensitive
  2. The file name separator in the path Windows uses a backslash, which is an escape character, and two backslashes represent a normal backslash

File class construction method

method one

File(String pathname) creates a new File instance by converting the given pathname string into an abstract pathname.
parameter:

String pathname: The path name of the string. The path
can end with a file or a folder. The
path can be a relative path or an absolute path. The
path can exist or not.
Create a File object, just put the characters The string path is encapsulated as a File object, regardless of whether the path is true or false

Way two

File(String parent, String child) creates a new File instance based on the parent path name string and the child path name string.
Parameter: divide the path into two parts
String parent: parent path
String child: child path
Benefits:
parent path and child path can be written separately, which is very flexible to use; both parent path and child path can be changed

Way Three

File(File parent, String child) creates a new File instance based on the parent abstract path name and the child path name string.
Parameter: divide the path into two parts
File parent: parent path
String child: child path
Benefits:
parent path and child path can be written separately, which is very flexible to use; both parent path and child path can be changed.
Parent path is of File type, and File can be used The method performs some operations on the path, and then uses the path to create an object

Way four

File(URL)

Common method

  • public String getAbsolutePath() : Return this FileAbsolute pathName string.
  • public String getPath() : Convert this File to a path name string.
  • public String getName(): Returns the name of the file or directory represented by this File. What you get is the end part (file/folder) of the transfer path of the constructor
  • public long length(): Returns the length of the file represented by this File. Get the specified method is to construct the file size , in bytes as a unit

note:

Folders have no concept of size,Can'tGet the size of the folder.
If the path given in the constructor does not exist, the length method returns0

Judgment method

public boolean exists(): Whether the file or directory represented by this File actually exists.
public boolean isDirectory(): This File indicates whether it is a directory.
public boolean isFile(): This File indicates whether it is a file.

note

There are only files/folders in the computer's hard disk. The two methods are mutually exclusive.
The premise of the two methods is that the path must exist, otherwise both return false

Create delete method

  • public boolean createNewFile() : If and only if the file with this name does not yet exist, create a new empty file.

The path and name of the created file are given in the constructor (parameters of the constructor)
Return value: Boolean
true: the file does not exist, create the file, return true
false: the file exists, will not be created, return false
Note:

  1. This method can only create files, not folders
  2. The path to create the file must exist, otherwise an exception will be thrown
  • public boolean delete() : Delete the file or directory represented by this File.

This method can delete the file/folder given in the path of the construction method.
Return value: Boolean
true: the file/folder is deleted successfully and returns true
false: there is content in the folder and will not be deleted. Return false; the path in the construction method There is no false
note:

The delete method is to directly delete files/folders** on the hard disk without going to the recycle bin**. Be careful when deleting

  • public boolean mkdir(): Create the directory represented by this File. Create a single-level empty folder
  • public boolean mkdirs(): Create the directory represented by this File, including any necessary but non-existent parent directories. You can create single-level empty folders or multi-level folders

The path and name of the created folder are given in the construction method (parameters of the construction method)
Return value: Boolean
true: the folder does not exist, create the folder, return true
false: the folder exists, will not be created, return false;
note:

This method can only create folders, not files

File directory traversal

File class traversal (folder) directory function

  • public String[] list() : Returns a String array, representing all sub-files or directories in the File directory.

Traverse the directory given in the construction method, will get all the files/folders in the directoryname, Store the obtained multiple names into a String type array

  • public File[] listFiles() : Returns a File array, representing all the sub-files or directories in the File directory.

Traverse the directory given in the construction method, will get all the files/folders in the directory, and put the files/foldersEncapsulated as a File object, Multiple File objects are stored in the File array

note:

listMethods and listFilesmethods traverse the directory given in the
constructor. If the path of the directory given in the constructor does not exist , a null pointer exception will be thrown.
If the path given in the constructor is not a directory , a null pointer will also be thrown. abnormal

FileFilter file filter

There are two ListFilesoverloaded methods in the File class. The parameter of the method is the filter
File[] listFiles(FileFilter filter)
java.io.FileFilterinterface: the filter used for the abstract path name (File object).
Function: Used to filter files (File object).
Abstract method: Used to filter files.
boolean accept(File pathname)Test whether the specified abstract path name should be included in a certain path name list.
Parameters:
File pathnameUse the ListFilesmethod to traverse the directory and get each file object
File[] listFiles(FilenameFilter filter)
java.io.FilenameFilterinterface: the class instance that implements this interface can be used to filter the file name.
Function: used to filter file name
Abstract method: used to filter file method to
boolean accept(File dir, String name)test whether the specified file should be included in a file list.
Parameters::
File dirThe traversed directory passed in the construction method
String name: Use the ListFilesmethod to traverse the directory and obtain the name of each file/folder
Note:

The two filter interfaces have no implementation classes. We need to write the implementation classes ourselves, **rewrite the filter method accept,** define the filter rules in the method.

Guess you like

Origin blog.csdn.net/ren9436/article/details/107592402