Java Foundation--file

Source: Internet
Author: User

1, create a file
File File = new file (path); File.createnewfile (); If the path does not exist, it throws an exception
File.mkdir ();//If the path does not exist, returns false, the directory is not created
File.mkdirs ();//If it is .../xx.txt, the Xx.txt directory is created
Note: If path does not have a write letter path, it will be created under the project path
2. deleting files
Delete ();
Note: Delete in Java does not go to recycle Bin
Files/folders cannot be included in folders when deleting a folder
3, renaming
File.renameto (File dest)//
4, judging
File.isdirectory ();
File.isfile ();
File.exists ();
File.canread ();
File.canwrite ();
File.ishidden ();
5, get
File.getabsolutepath ();
File.getpath ();
File.getname ();
Long length = File.length ();//byte number length/1024/1024 = M
Loong time = file.lastmodified ();//The millisecond value of the last modification
Note: How to convert a millisecond value to a date
Date d = new date (time);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:MM:SS")
String lastmodifiedtime = Sdf.format (d);

String[] List ();//array of names
File[] Listfiles ();//file Array (File.getname get name)

Example:

Determine if there is a. jpg in the directory, and if so, the output
static void GetJpgName2 (String path) {

File File = new file (path);

string[] Namearr = file.list (new FilenameFilter () {

@Override
Public Boolean Accept (File dir, String name) {

return new File (dir, name). Isfile () && name.endswith (". jpg");
}
});

for (String Name:namearr) {System.out.println (name);}}

Java Foundation--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.