Java File objects are commonly used for File operations (recommended ).

Source: Internet
Author: User

Java File objects are commonly used for File operations (recommended ).

File objects are the most commonly used classes for File operations. They are usually used in a lot of work, and I have posted several common methods for my work. Summary:

Directly run the Code:

// Construct the File object file = new File ("E:/android_demo/a"); File fileTest = new File ("E:/android_demo/a/test.mp3 "); // obtain the File's parent path file f = File. getParentFile (); System. out. println ("f =" + f); // E: \ android_demo // determine whether the file exists boolean is = file. exists (); // does not exist, return fasleSystem. out. println ("is =" + is); // obtain the absolute path of the object, which is equivalent to getPathString path1 = file. getAbsolutePath (); System. out. println ("path =" + path1); // E: \ android_demo \ a // obtain the file path String path2 = file. getPath (); System. out. println ("path2 =" + path2); // E: \ android_demo \ a // get the current file name String s = file. getName (); System. out. println ("s =" + s); // create a folder, that is, E:/android_demo/afile. mkdir (); // create a file, that is, E:/android_demo/a/test1_filetest. createNewFile (); // file size, number of bytes occupied during file storage; long l = f. length (); System. out. println ("l =" + l); // obtain the file path stringString str = fileTest. toString (); System. out. println ("str =" + str); // E: \ android_demo \ a \ test.pdf // rename the File fileTest2 = new File ("E: /android_demo/a/test2.mp3 "); boolean b2 = fileTest. renameTo (fileTest2); System. out. println ("b2 =" + b2); // E:/android_demo/a/test2.mp3 // delete the file boolean b3 = fileTest. delete (); // delete a folder. Note that only files in the deleted folder can be deleted. If yes, you must delete all files before deleting boolean b4 = file. delete ();

The above several common methods (recommended) for File operations on File objects in java are all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference, we also hope that you can support the customer's home.

Related Article

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.