First, brief
File file class, mainly related to the operation of files. Commonly used file operations are: the creation of files (folders), the deletion of files (folders), the file read and download (copy) and so on;
Ii. creation and deletion of files (clips)
1. Create a Process instance:
//Create a new File@Test Public voidTestcreatefile () {File m=NewFile ("E://file");//Create a folder//determine if the folder exists no if(!m.exists ()) {M.mkdir ();//Create a folder} File F=NewFile ("E://file//test.txt");//Creating Documents if(!f.exists ()) { Try{f.createnewfile (); //Create a file}Catch(IOException e) {e.printstacktrace (); } } }
2. Delete process
//Delete a file//@Test Public voidTestdeletefile () {File F=NewFile ("E://file//test.txt"); if(F.exists ()) {f.delete (); } } //Delete a folder and all files below [using recursion method] Public Static voidtestdelallfile (String path) {File m=NewFile (path); file[] Files=M.listfiles (); for(File f:files) {if(F.isfile ()) {f.delete (); }Else if(F.isdirectory ()) {Testdelallfile (F.getpath ()); }} m.delete (); //It's a good idea to delete folders } Public Static voidMain (string[] args) {Filetest.testdelallfile ("E://file"); }
Java file action one: creation and deletion of files