Java Basic action methods for files and folders summary __java files common operations

Source: Internet
Author: User

1. Create a folder or file reference:

File F = new file ("folder or file path");

Supplemental Note: The file or folder path is divided into absolute and relative paths

1> absolute Path: All file paths, including disk characters, specific directories, etc.;

2> relative path: Relative to a file path, for example, relative to Java engineering;

2, the creation of reference to the corresponding file:

F.createnewfile ();

3, create a reference to the corresponding folder:

F.mkdir (); Creating a single-tier directory

F.mkdirs (); Creating a multi-tiered Directory

4, to determine whether the reference to the corresponding file or folder exists:

F.exists ();

5, delete references to the corresponding file or folder:

F.delect (); When you delete a folder and there are subdirectories in the folder, you cannot delete it;

6, to determine whether the reference is a folder:

F.isdirectory ();

7, to determine whether the reference is a file:

F.isfile ();

8, to determine whether the reference file or folder is hidden:

F.ishidden ();

9, to determine whether the reference to the corresponding file or folder is readable:

F.canread ();

10, to determine whether the reference file or folder can be written:

F.canwrite ();

11, to determine whether the reference file or folder is executable:

F.canexecute ();

12, get reference to the corresponding file or folder size:

F.length (); The folder gets the method to get the size inaccurate, which requires the file clip directory to traverse the file size summation;

13. Get the absolute path to refer to the corresponding file or folder:

F.getpath ();

F.getabsolutepath ();

14. Get the parent directory that references the corresponding file or folder:

F.getparent ();

15, get references to the corresponding file or folder name:

F.getname ();

16. Get a collection of child files or subfolders that refer to the corresponding folder:

file[] flist = F.listfiles ();

eg. Get a. txt file that references the corresponding file

file[] Flist = f.listfiles (new FileFilter () {

Public Boolean accept (File pathname) {

Return Pathname.getname (). Endwith (". txt");

}

})

Where the anonymous object is filefilter as a file filter.

17. Rename the reference file or folder:

F.renameto (File F2);


The following is my use of the above methods to write a few of the folder operation of the packaging method links, including;

1, get all the files in the folder

2. Get all the files in the folder with the suffix of specific characters

3. Delete Folder

4. Cut Folder

Interested readers can click to read. Click to open the link


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.