File object is the most commonly used class of files, usually work a lot, posted a few I work commonly used several methods. Briefly summed up the next
Directly on the code:
The build file Object file File=new file ("e:/android_demo/a");
File Filetest=new file ("E:/android_demo/a/test.mp3");
Gets the file's parent path file F=file.getparentfile (); System.out.println ("f==" +f);//e:\android_demo//Judge whether the file exists a Boolean is=file.exists ();//does not exist, returns Fasle System.out.println
("is==" +is);
Getting the absolute path of a file can be understood as equivalent to GetPath String Path1=file.getabsolutepath ();
System.out.println ("path==" +path1);//e:\android_demo\a//Get file path String Path2=file.getpath ();
System.out.println ("path2==" +path2);//e:\android_demo\a//get current filename String s=file.getname ();
System.out.println ("s===" +s);
Create a folder, namely: e:/android_demo/a File.mkdir ();
Create a file, namely: E:/android_demo/a/test.mp3 Filetest.createnewfile ();
File size, the number of bytes occupied by the file when it was stored; long l=f.length ();
System.out.println ("l==" +l);
Gets the file path string string str=filetest.tostring (); System.out.println ("str==" +str);//E:\android_demo\a\test.mp3 to rename file filetest2=new files ("e:/android_demo/a/
Test2.mp3 ");
Boolean B2=filetest.renameto (FILETEST2); System.out.println ("b2==" +b2);//e:/android_demo/A/test2.mp3//Delete file Boolean b3=filetest.delete (); Delete the folder, note that the deleted folder must have no files to delete, some words to facilitate the deletion of all files, and then delete the Boolean b4=file.delete ();
Above this Java file object to the operation of a number of common methods (recommended) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.