Java I/O (I) File class

Source: Internet
Author: User

The input/output stream class processes the reading and writing of file content. The file class indicates the storage information of files and folders, such as the file location and size.

Three types of file structure

File (File parent, string child) file (string pathname) file (string parent, string child)
String Path = "D:" + file. separator + "filetest"; file = new file (PATH); system. out. println (File); // constructed file object. The imported file object is used as the root directory file file2 = new file (file, "subfile"); system. out. println (file2); // constructed file object. The passed String object is used as the root directory file file3 = new file (path, "subfile"); system. out. println (file3 );
Common Methods of the file class: getpath () returns the string of the path, which is equivalent to the tostring () method of the file class. getname () returns the file name, excluding the path exists () whether mkdir () exists in the file/directory: create a level 1 subdirectory under the existing path

Mkdirs () can be used to create a multi-layer directory in an existing path. For example, create the D:/ffffff/filetest/sub directory in drive D.
String path2 = "D:" + File.separator + "ffffff" +File.separator + "filetest" + File.separator+ "sub";File file = new File(path);file.mkdirs();

File. mkdir () cannot create a directory with a layer-4 structure such as D:/ffffff/filetest/Sub. file. mkdirs () can
Whether isfile () is a file
Whether isdirectory () is a directory

Length () returns the length measured in bytes.
Createnewfile () create a file
Getparent () returns the string of the parent directory, the file returns its parent directory, the directory returns the parent directory, and the root directory returns NULL

Getparentfile () returns the file object of the parent directory, same

Renameto (File file) can be renamed, you can change the suffix, such as 1.txt to 2.dat

Standard Path Name Of The GetCanonicalPath () File

Getcanonicalfile () file standard path file object

Delete () delete a file, non-directory

List () lists all level 1 folder names and file names under the file object.

Example: Directory D:/filetestwith folder1folder, test.txt,test2.txt

String path = "D:" +File.separator + "filetest";File file = new File(path);for (String s : file.list())System.out.println(s);

Result: folder1test.txttest2.txt


Corresponding to listfiles (), usage
for (File s : file.listFiles())System.out.println(s);

List (filenamefilter filter) returns the array of file names that meet the filtering conditions of the filenamefilter interface. Usage: Write a class to implement the filenamefilter interface, override the accept method, and write the file filtering logic in the accept.

Example: returns a TXT file in the specified directory.
Class extensionfilter implements filenamefilter {public extensionfilter (string ext) {extension = ". "+ ext ;}@ overridepublic Boolean accept (File Dir, string name) {return name. endswith (Extension);} private string extension ;}..... in the main method: For (string S: file. list (New extensionfilter ("TXT") system. out. println (s );

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.