Java file class methods

Source: Internet
Author: User

Java file class methods

File class:

The File class is a platform-independent File and directory under the java. io package. That is to say, if you want to operate files and directories in the program, you can use the File class. But File cannot access the File content itself.

Access files and directories:

1. Partial methods for accessing File Names

String getName (): returns the File name or path name represented by this File object (if it is a path, the last level of sub-path name is returned ).

String getPath (): returns the path name of the File object.
File getAbsoluteFile (): returns the absolute path of the File object.

String getAbsolutePath (): returns the absolute path name of the File object.

String getParent (): returns the parent name of the directory (last level subdirectory) corresponding to the File object.

Boolean renameTo (File newName): Rename the File or directory corresponding to the File object. If the name is successfully renamed, true is returned; otherwise, false is returned.

2. File Detection Methods

Boolean exists (): determines whether the File or directory corresponding to the File object exists.

Boolean canWrite (): determines whether the File or directory corresponding to the File object can be written.

Boolean canRead (): determines whether the File or directory corresponding to the File object is readable.
Boolean isFile (): determines whether the File object corresponds to a File rather than a directory.
Boolean isDirectory (): determines whether the File object corresponds to a directory rather than a File.

Boolean isAbsolute (): determines whether the File or directory corresponding to the File object is an absolute path. This method eliminates the differences between different platforms and can directly determine whether the File object is an absolute path.

3. Get common file information

Long lastModified (): The last modification time of the returned object.
Long length (): the length of the returned object content.

4. File Operations related methods

Boolean createNewFile (): If the File corresponding to this File object does not exist, this method creates a new File specified by this File. If the File is created successfully, true is returned; otherwise, false is returned.

Boolean delete (): delete the File or path corresponding to the File object.

Static File createTempFile (String prefix, String suffix): create a temporary empty File in the default temporary File directory, and use the given prefix, random number generated by the system, and the given suffix as the File name. This is a static method that can be called directly through the File class. The Prefix parameter must be at least 3 bytes long. We recommend that you use a short and meaningful string with the suffix, such as "hjb" or "mail ". The Suffix parameter can be null. In this case, the default Suffix ". tep" is used ".

Static File createTempFile (String prefix, String suffix, File directory): create a temporary empty File in the directory specified by directory, use the given prefix, the random number generated by the system, and the given suffix as the file name. This is a static method that can be called directly through the File class.

Void deleteOnExit (): registers a deletion hook to delete the files and directories corresponding to the File object when the Java Virtual Machine exits.

5. Directory operations

Boolean mkdir (): attempts to create a directory corresponding to the File object. If the File object is created successfully, true is returned; otherwise, false is returned. The File object that calls this method must correspond to a path rather than a File.

String [] list (): lists all the sub-File names and path names of the File object, and returns a String array.

File [] listFiles (): lists all the sub-File names and paths of the File object, and returns the File array.

Static Filep [] listRoots (): list all root paths of the system. This is a static method that can be called directly through the File class.

File filter:

A FilenameFilter parameter can be accepted in the list () method of the File class. This parameter can be used to list only files that meet the conditions.

The FilenameFilter Interface contains an accept (File dir, String name) method. This method iterates all subdirectories or files of the specified File in sequence. If this method returns true, the list () method will list this subdirectory or file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.