I. Overview
Reasons for using this class:
This class encapsulates a file or folder as an object.
Facilitates the operation of File and folder property information.
The file object is passed as a parameter to the constructor of the stream.
Requirement: Use common methods of the file class.
Under Windows platform, the directory delimiter is \, while Linux is/, in the string, the \ symbol needs to use \ \, the first \ Escape function. This delimiter can be used System.getproperty ("File.separator");
Second, the commonly used methods.
1. Construction method.
Construction Method Summary |
File(File parent, String child) Creates a new instance from the parent abstract path name and child pathname string File . |
File(String pathname) Creates a new instance by converting the given pathname string to an abstract path name File . |
File(String parent, String child) Creates a new instance based on the parent pathname string and the child pathname string File . |
File(URI uri) Creates a new file instance by converting the given file: URI to an abstract path name. |
The file actually includes the files we usually say, such as media files, text files, and so on, as well as directories, which we usually call folders.
Examples of construction methods:
"1" uses the absolute path of the file as the constructor parameter.
File file=new file ("C://aim.txt");
"2" uses the directory path as the constructor parameter.
File file=new file ("C:/windows");
"3" uses the path + file name as the construction method parameter
File file=new file (new file ("C://windows"), "Aim.txt");
It should be explained that the third method is more flexible in the construction of one of the parameters can be changed to construct different directories of the construction of the file object and under the same directory under different files under the construction of the object.
2. Common methods.
(1) obtain.
"1" Gets the file name and path
public string getName () Description: The string returned by the method is actually a file name that does not contain a path prefix.
-
Returns the name of the file or directory represented by this abstract path name. The name is the last name in the sequence of pathname names. If the path name sequence is empty, an empty string is returned.
|
public String GetPath ()
-
Converts this abstract path name to a pathname string. The resulting string uses the
default name delimiter to separate the names in the name sequence.
-
Description: The method essentially returns the string that was filled in the constructed method intact. (including absolute and relative paths)
|
public String GetAbsolutePath ()
returns the absolute pathname string for this abstract pathname. Description: This method returns an absolute path string for a file object that is constructed using either an absolute or a relative path.
If this abstract pathname is already an absolute pathname, the pathname string is returned, as is the getpath () method. If this abstract pathname is an empty abstract pathname, the pathname string for the current user directory is returned, which is specified by the system property, User.dir . Otherwise, the path name is resolved in a system-related manner. On a UNIX system, the relative pathname is resolved according to the current user directory, which makes the pathname an absolute path name. On a Microsoft Windows system, the relative pathname is resolved by the current drive directory specified by the pathname, if any, to make the pathname an absolute pathname, otherwise it can be resolved based on the current user directory.
|
There is also a less common method:
Getabsolutefile ()
-
Returns the absolute pathname form for this abstract path name. Equal to
new File(this.getAbsolutePath() ) .
-
-
-
Return:
-
An absolute abstract path name that represents the same file or directory as this abstract pathname
|
Observing the method, the method return value is a file object and is actually a file object created using the string returned by the GetAbsolutePath method.
"2" gets the file size
"3" Get File modification time
"JAVA File Overview"