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.