The user interface and operating system name the files and directories using the system-related pathname string. This class renders an abstract, system-independent view of the hierarchical path name. The abstract path name has two components:
An optional system-related prefix string, such as a drive letter, "/" for the root directory in UNIX, "\\\\" for the Microsoft Windows UNC pathname, and
A sequence of 0 or more string names.
In addition to the last one, each name in the abstract pathname represents a directory, and the last name can represent either a directory or a file. An empty abstract path name does not have a prefix and a name sequence.
The conversion between the pathname string and the abstract path name is system-related. When you convert an abstract path name to a pathname string, each name is separated from the next name by a single default delimiter character. The default name delimiter is defined by the system property File.separator, and can also be obtained from the public static fields separator and Separatorchar of this class. When you convert a pathname string to an abstract pathname, you can use either the default name delimiter or any other name separators supported by the underlying system to separate the names.
Either an abstract pathname or a string pathname, either an absolute pathname or a relative path name. The absolute pathname is the full pathname, and it does not require any additional information to locate the file that it represents. Instead, the relative pathname must be interpreted using information from other pathname names. By default, the classes in the Java.io package always parse relative path names based on the current user directory. This directory is specified by the system property User.dir, which is usually the calling directory for the Java virtual machine.
The concept of a prefix is used to process the root directory of the UNIX platform, as well as the drive letter, root directory, and UNC pathname on the Microsoft Windows platform, as follows:
For UNIX platforms, the absolute pathname prefix is always "/". There is no prefix for relative path names. The absolute pathname of the root directory is prefixed with "/" and there is no name sequence.
For the Microsoft Windows platform, the prefix of the path name that contains the drive letter consists of the driver name and a ":": If the pathname is an absolute pathname, then "\ \" may be followed. The UNC pathname is prefixed with "\\\\", and the host name and share name are the first two names in the name sequence. The relative path name of the drive is not specified without prefix.
An instance of the file class is immutable; that is, once created, the abstract path name represented by the file object will never change.
Construction Method:
File (file parent, String child)
Creates a new File instance based on the parent abstract pathname and child pathname string.
File (String pathname)
Creates a new File instance by converting the given pathname string to an abstract path name.
File (string parent, String child)
Creates a new File instance based on the parent pathname string and the child pathname string.
File (URI Uri)
Creates a new file instance by converting the given File:uri to an abstract path name.
Method Details:
GetName
Public String GetName () returns the name of the file or directory represented by this abstract path name. The name is the last name in the name sequence of the pathname. If the name sequence of the pathname is empty, an empty string is returned.
Return:
This abstract pathname represents the name of the file or directory and returns an empty string if the name sequence of the pathname is empty
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Etparent
public string GetParent () returns the pathname string for the parent path name of this abstract pathname, or null if the pathname does not specify a parent directory.
The parent path name of an abstract pathname consists of the prefix of the pathname (if any) and all names except the last name in the sequence of pathname names. If the name sequence is empty, then the path name does not specify a parent directory.
Return:
The path name string of the parent directory specified by this abstract pathname, or null if the pathname does not specify a parent directory
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Getparentfile
Public File Getparentfile () returns the abstract path name of the parent pathname of this abstract pathname, or null if the pathname does not specify a parent directory.
The parent path name of an abstract pathname consists of the prefix of the pathname (if any) and all names except the last name in the sequence of pathname names. If the name sequence is empty, then the path name does not specify a parent directory.
Return:
The abstract path name of the parent directory specified by this abstract pathname, or null if the pathname does not specify a parent directory
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
GetPath
public string GetPath () converts this abstract pathname to a path name string. The resulting string uses the default name delimiter to separate names in the name sequence.
Return:
The string form of this abstract path name
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Isabsolute
public boolean Isabsolute () tests whether this abstract path name is an absolute pathname. The definition of absolute pathname is related to the system. On UNIX Systems, if the pathname is prefixed with "/", the pathname is the absolute path name. On a Microsoft Windows system, if the pathname is prefixed with a drive letter followed by "\ \", or if the prefix is "\\\\", the pathname is an absolute path name.
Return:
Returns true if this abstract pathname is an absolute path name; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
GetAbsolutePath
public string GetAbsolutePath () returns the absolute pathname string for the abstract path name.
If this abstract pathname is already an absolute pathname, the pathname string is returned, just like 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, this pathname is parsed using a system-related approach. On UNIX systems, by parsing a relative pathname based on the current user directory, you can make the pathname an absolute path name. On a Microsoft Windows system, a relative pathname is parsed by the current drive directory specified by the pathname, if any, to make the pathname an absolute pathname, otherwise it can be parsed based on the current user directory.
Return:
An absolute pathname string that represents the same file or directory as this abstract path name.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Getabsolutefile
Public File Getabsolutefile () returns the absolute pathname form of the abstract path name. equals the new File (This.getabsolutepath ()).
Return:
Represents the absolute abstract pathname of a file or directory that is the same as this abstract path name
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Getcanonicalpath
public string Getcanonicalpath () throws IOException returns the canonical path name string for the abstract pathname.
Canonical path names are absolute pathname and are unique. The exact definition of canonical path names is related to the system. If necessary, this method first converts the pathname to an absolute pathname, as is the effect of invoking the GetAbsolutePath () method, and then maps it to its unique pathname in a system-dependent manner. This usually involves removing redundant names from the pathname (such as "." and "..") ), analyze symbolic connections (for UNIX platforms), and convert drive names to standard case (for Microsoft Windows platforms).
Each path name that represents an existing file or directory has a unique canonical form. Each pathname that represents a non-existent file or directory also has a unique canonical form. The canonical form of a non-existent file or directory pathname may be different from the canonical form of the same pathname after the file or directory was created. Similarly, the canonical form of an existing file or directory pathname may be different from the canonical form of the same pathname after the file or directory is deleted.
Return:
A canonical pathname string that represents the same file or directory as this abstract path name
Thrown:
IOException-If an I/O error occurs (possibly because a file system query is required to construct the canonical pathname)
SecurityException-If the desired system property value cannot be accessed, or if a security manager exists, and its securitymanager.checkread (Java.io.FileDescriptor) method denies read access to the file
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Getcanonicalfile
Public File Getcanonicalfile () throws IOException returns the canonical form of this abstract path name. equals the new File (This.getcanonicalpath ()).
Return:
A canonical pathname string that represents the same file or directory as this abstract path name
Thrown:
IOException-If an I/O error occurs (possibly because a file system query is required to construct the canonical pathname)
SecurityException-If the desired system property value cannot be accessed, or if a security manager exists, and its securitymanager.checkread (Java.io.FileDescriptor) method denies read access to the file
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Tourl
Public URL Tourl ()
Throws Malformedurlexception converts this abstract pathname to a file:url. The specific form of the URL is related to the system. If you can determine that the file represented by this abstract pathname is a directory, the resulting URL will end with a slash.
Usage considerations: This method does not automatically circumvent illegal characters in URLs. It is recommended that the new code convert the abstract pathname to a URL by first converting the abstract pathname to a URI through the Touri method, and then converting the URI to a URL through the Uri.tourl method.
Return:
A URL object that represents the equivalent file URL
Thrown:
Malformedurlexception-If the path cannot be parsed as a URL
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Touri
The public URI Touri () constructs a file:uri that represents this abstract pathname.
The specific form of the URI is related to the system. If you can determine that the file represented by this abstract pathname is a directory, the resulting URI will end with a slash.
For a given abstract path name F, you can guarantee:
New File (F.touri ()). Equals (F.getabsolutefile ())
As long as the original abstract pathname, URI, and new abstract path name are created in the same Java virtual machine (or its different calls). However, because of the system-specific nature of the abstract pathname, this relationship is usually not true when the File:uri created in a virtual machine on an operating system is converted to an abstract pathname in a virtual machine on a different operating system.
Return:
An absolute hierarchical URI consisting of a scheme that equals "file", a non-empty path component, and an undefined authority, query, and fragment component
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
CanRead
The public boolean canRead () tests whether the application can read the file represented by this abstract path name.
Return:
Returns true if and only if the file specified by this abstract pathname exists and can be read by the application; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
CanWrite
public boolean canWrite () tests whether the application can modify the file represented by this abstract path name.
Return:
Returns true if and only if the file system actually contains the file represented by this abstract pathname and allows the application to write to the file, otherwise false is returned.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Exists
public Boolean exists () tests whether the file or directory represented by this abstract pathname exists.
Return:
Returns true if and only if the file or directory represented by this abstract pathname is present; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Isdirectory
public boolean isdirectory () tests whether the file represented by this abstract path name is a directory.
Return:
Returns true if and only if the file represented by this abstract pathname exists and is a directory;
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Isfile
public boolean isfile () tests whether the file represented by this abstract path name is a standard file. If the file is not a directory and other system-related standards are met, the file is a standard file. A non-catalog file created by a Java application must be a standard file.
Return:
Returns true if and only if the file represented by this abstract pathname is present and is a standard file;
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Ishidden
public boolean Ishidden () tests whether the file specified by this abstract path name is a hidden file. The specific definition that is hidden is related to the system. On UNIX systems, if the file name begins with a period character ('. '), it is considered to be hidden. On a Microsoft Windows system, if the file is marked as hidden in the file system, the file is considered to be hidden.
Return:
Returns True when and only if the file represented by this abstract pathname is hidden based on the underlying platform convention
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
LastModified
Public long LastModified () returns the last time that the file was modified by this abstract pathname.
Return:
A Long value representing the time at which the file was last modified, measured in milliseconds, with a time difference of 00:00:00 GMT (January 1, 1970). If the file does not exist, or an I/O error occurs, the 0L is returned
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Length
public long Length () returns the length of the file represented by this abstract path name. If this path name represents a directory, the return value is indeterminate.
Return:
This abstract pathname represents the length of the file, in bytes, and returns 0L if the file does not exist
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
CreateNewFile
public boolean createnewfile ()
Throws IOException a new empty file specified by this abstract pathname is created atomically when and only if there is no file with the name specified by this abstract pathname. Checks whether the file exists, creates the file if it does not exist, is a single action, and is atomic for all other file system activities that may affect the file.
Note: This method does not apply to file locking because the resulting protocol may not work reliably. The filelock mechanism should be used instead.
Return:
Returns true if the specified file does not exist and is successfully created, or False if the specified file already exists
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Delete
public boolean Delete () deletes the file or directory represented by this abstract path name. If this pathname represents a directory, the directory must be empty to be deleted.
Return:
Returns true if and only if the file or directory is deleted successfully; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Deleteonexit
public void Deleteonexit () requests that the file or directory represented by this abstract path name be deleted when the virtual machine terminates. The delete operation is attempted only when the virtual machine terminates normally, as defined in Java Language specification.
Once a delete operation has been requested, the request cannot be canceled. Therefore, you should use this method with caution.
Note: This method does not apply to file locking because the resulting protocol may not work reliably. Filelock facilities should be used instead.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
List
Public string[] List () returns an array of strings consisting of the names of the files and directories in the directory represented by this abstract path name.
If this abstract path name does not represent a directory, this method returns NULL. Otherwise, returns an array of strings for each file or directory in the directory. Indicates that the name of the directory itself and its parent directory are not included in the results. Each string is a file name, not a full path.
It is not guaranteed that the same strings in the resulting array will appear in a particular order, especially if they are not guaranteed to appear in alphabetical order.
Return:
An array of strings consisting of the names of the files and directories in the directory represented by this abstract pathname. If the directory is empty, the array will also be empty. Returns null if the abstract path name does not represent a directory, or an I/O error occurs.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
List
Public string[] List (FilenameFilter filter) returns an array of strings consisting of the names of the files and directories contained in the directory, represented by the abstract path name that satisfies the specified filter. This method behaves the same as the list () method, except that the string in the returned array must satisfy the filter. If the given filter is NULL, all names are accepted. Otherwise, the name satisfies the filter only if the Filenamefilter.accept (Java.io.File, java.lang.String) method that invokes the filter on this abstract pathname and the file name or directory name in the directory it represents is returned true.
Parameters:
Filter-File name filter
Return:
An array of strings of files and directories in the directory represented by this abstract pathname, which is accepted by the given filter. If the directory is empty, or if no name is accepted by the filter, the array will be empty. Returns null if the abstract path name does not represent a directory, or an I/O error occurs.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Listfiles
Public file[] Listfiles () returns an abstract path an array group that represents the file in the directory represented by this abstract pathname.
If this abstract path name does not represent a directory, this method returns NULL. Otherwise, returns an array of file objects for each of the files or directories in the directory. The path name that represents the directory itself and its parent directory is not included in the results. Each abstract path name obtained is constructed using the file (file, String) construction method, based on this abstract pathname. So, if this pathname is an absolute pathname, each path name you get is an absolute pathname, and if the pathname is a relative pathname, each path name you get is a pathname relative to the same directory.
It is not guaranteed that the same strings in the resulting array will appear in a particular order, especially if they are not guaranteed to appear in alphabetical order.
Return:
Represents the abstract path to the an array group of files and directories in the directory represented by this abstract path name. If the directory is empty, the array will also be empty. Returns null if the abstract path name does not represent a directory, or an I/O error occurs.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Listfiles
Public file[] Listfiles (filenamefilter filter) returns an abstract path an array group that represents the files and directories in the directory represented by this abstract pathname, which satisfies a specific filter. This method behaves the same as the Listfiles () method, except that the path name in the returned array must satisfy the filter. If the given filter is NULL, all pathname names are accepted. Otherwise, the pathname satisfies the filter only if the Filenamefilter.accept (Java.io.File, java.lang.String) method that invokes the filter on this abstract pathname and the file name or directory name in the directory it represents is returned true.
Parameters:
Filter-File name filter
Return:
Represents the abstract path to the an array group of files and directories in the directory represented by this abstract path name. If the directory is empty, the array will also be empty. Returns null if the abstract path name does not represent a directory, or an I/O error occurs.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Listfiles
Public file[] Listfiles (filefilter filter) returns an abstract path an array group that represents the files and directories in the directory represented by this abstract pathname, which satisfies a specific filter. This method behaves the same as the Listfiles () method, except that the path name in the returned array must satisfy the filter. If the given filter is NULL, all pathname names are accepted. Otherwise, the path name satisfies the filter if and only if the Filefilter.accept (Java.io.File) method that invokes the filter on the pathname returns TRUE.
Parameters:
Filter-File filter
Return:
Represents the abstract path to the an array group of files and directories in the directory represented by this abstract path name. If the directory is empty, the array will also be empty. Returns null if the abstract path name does not represent a directory, or an I/O error occurs.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Mkdir
public boolean mkdir () creates the directory specified by this abstract path name.
Return:
Returns true if and only if the directory was created; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Mkdirs
public Boolean mkdirs () creates the directory specified by this abstract pathname, including creating a required but nonexistent parent directory. Note that if this operation fails, some of the required parent directories may have been created successfully.
Return:
Returns true if and only if the directory and all required parent directories have been created; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Renameto
The public boolean renameto (file dest) renames the file represented by this abstract path name.
Many aspects of this method behavior are platform-related: The rename operation cannot move a file from one file system to another, the operation may not be atomic, and the operation may not succeed if a file with the target abstract pathname already exists. You should always check the return value to ensure that the rename operation succeeds.
Parameters:
Dest-Specifies the new abstract path name of the file
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Setlastmodified
public boolean setlastmodified (long) sets the last modification time of the file or directory specified by this abstract path name.
All platforms support setting the file modification time to the nearest number of seconds, but some platforms provide more precise values. The parameter will be truncated to accommodate the supported precision. If the set operation succeeds and no other interference occurs on the file, the next call to the LastModified () method returns the time parameter passed to the method (which may be intercepted).
Parameters:
Time-the date of the last modification, measured in milliseconds, with the time difference between the date and the calendar (January 1, 1970, 00:00:00 GMT)
Return:
Returns true if and only if the operation succeeds; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Setreadonly
public boolean setreadonly () marks the file or directory specified by this abstract pathname so that it can be read only. After you call this method, you can guarantee that the file or directory will not change until it is deleted or marked for write access. Whether a read-only file or directory can be deleted depends on the underlying system.
Return:
Returns true if and only if the operation succeeds; otherwise false
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Listroots
public static file[] Listroots () lists the available file system root directories.
A specific Java platform can support 0 or more file systems organized hierarchically. Each file system has a root directory from which you can reach all other files in the file system. For example, the Windows platform provides a root directory for each active drive, and the UNIX platform has only one root directory, or "/". The settings of the available file system root directory are affected by various system-level operations, such as inserting and ejecting removable media, and disconnecting or uninstalling those physical or virtual disks.
This method returns an array of file objects that represent the root directory of the available filesystem root. The canonical path name of any file that can be guaranteed to be physically present on the local machine begins with one of the root directories returned by this method.
The canonical pathname of a file located on some other machine is accessed through a remote file system protocol such as SMB or NFS, which may or may not be the beginning of one of the root directories returned by this method. If the path name of the remote file cannot be syntactically differentiated from the pathname of the local file, it will begin with one of the root directories returned by this method. For example, this method returns a File object that represents the root directory on the Windows platform that is mapped to a network drive, and the file object that contains the UNC pathname is not returned by this method.
Unlike most methods in this class, this method does not throw a security exception. If a security manager exists and its securitymanager.checkread (java.lang.String) method denies read access to a specific root directory, the directory is not displayed in the results.
Return:
Represents an array of file objects that are available at the root of the filesystem and returns null if the root set cannot be determined. If there is no file system root, the array will be empty.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Createtempfile
public static file Createtempfile (String prefix,string suffix,file directory) throws IOException creates a new empty file in the specified directory. Generates its name using the given prefix and suffix string. If this method returns successfully, you can guarantee:
The file represented by the returned abstract path name does not exist before this method is called, and
Neither this method nor any of its variants will return the same abstract pathname when the virtual machine is currently called.
This method only provides some of the functionality of the temporary file. To schedule files created by this method to be automatically deleted, use the Deleteonexit () method.
The prefix parameter must be at least three bytes long. It is recommended that the prefix use a short, meaningful string, such as "HJB" or "mail". The suffix parameter may be null, in which case the suffix ". tmp" is used.
To create a new file, you may want to first adjust the prefix and suffix so that it meets the limitations of the underlying platform. If the prefix is too long, it is truncated, but the first three characters are always preserved. If the suffix is too long, it is truncated, but if it starts with a period character ('. '), then the period and the three characters followed by it will always be preserved. Once these adjustments have been made, the name of the new file will be generated from the connection prefix, five or more internally generated characters and suffixes.
If the directory parameter is NULL, the system-related default temp file directory is used. The default temporary file directory is specified by the system property Java.io.tmpdir. On UNIX systems, the default value for this property is usually "/tmp" or "/var/tmp"; on Microsoft Windows systems, this value is typically "c:\\winnt\\temp". When calling a Java virtual machine, you can provide different values for this system property, but if other programs change this property, there is no guarantee that this will have any effect on the temporary directory used by this method.
Parameters:
Prefix-the prefix string used to generate the file name; must be at least three characters in length
suffix-the suffix string used to generate the file name, possibly null, in which case the suffix ". tmp" will be used
Directory-the folder where the file will be created, or null if the default temp file directory is used
Return:
Abstract path name representing the newly created empty file
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Createtempfile
public static file Createtempfile (String prefix,string suffix) throws IOException creates an empty file in the default temporary files directory, using the given prefix and suffix to generate its name. Calling this method is equivalent to calling createtempfile (prefix, suffix, null).
Parameters:
Prefix-the prefix string used to generate the file name; must be at least three characters in length
suffix-the suffix string used to generate the file name, possibly null, in which case the suffix ". tmp" will be used
Return:
Abstract path name representing the newly created empty file
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
CompareTo
The public int compareTo (File pathname) compares the two abstract path names in alphabetical order. The order in which this method is defined depends on the underlying system. On UNIX systems, when comparing pathname, letter capitalization is often important, and on Microsoft Windows systems, this is usually not important.
Designated by:
CompareTo in the interface comparable<file>
Parameters:
Pathname-Abstract path name to compare with this abstract path name
Return:
Returns 0 if the parameter equals this abstract pathname, or a value less than 0 if the abstract pathname is less than this parameter in alphabetical order, or a value greater than 0 if the abstract pathname is greater than this parameter in alphabetical order
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
equals
public boolean equals (object obj) tests whether this abstract path name is equal to a given object. Returns true if and only if the parameter is not NULL and is an abstract pathname that represents the same file or directory as this abstract path name. The two abstract path names are equal depending on the underlying system. On UNIX systems, when comparing pathname, letter capitalization is often important, and on Microsoft Windows systems, this is usually not important.
Covered:
equals in the class Object
Parameters:
Obj-the object to compare with this abstract pathname
Return:
Returns true if and only if the object is the same;
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Hashcode
public int hashcode () computes the hash code for this abstract pathname. Because the equivalence of the abstract pathname is related to the system, the calculation of its hash code is also related to the system. On UNIX systems, the hash code of the abstract pathname equals the XOR of its pathname string and the hash code of the decimal value 1234321. On a Microsoft Windows system, the hash code is equal to the XOR of its converted lowercase pathname string and the hash code of the decimal value 1234321.
Covered:
Hashcode in the class Object
Return:
Hash code for this abstract path name
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Tostring
The public string toString () returns the pathname string for this abstract path name. The string is the string returned by the GetPath () method.
Introduction to the Java file class