ImportJava.io.*; Public classFileoperate {//Create the specified directory voidmakedirectory (String dirName) {File file=NewFile (DirName);//instantiating a File object Try{ if(File.exists ()) {System.out.println (DirName+ "already exists"); } Else{file.mkdir (); System.out.println (DirName+ "was created!"); } }Catch(Exception e) {e.printstacktrace (); } } //empties all files in the specified directory voidemptydirectory (String dirName) {File file=NewFile (DirName);//instantiating a File object if(File.isdirectory ())//If the file object is a folder{File f[]= File.listfiles ();//Remove all files from this folder for(inti=0;i<f.length;i++) {f[i].delete (); } } Else{//Delete a file if it is not a folderFile.delete (); } } //Lists all file recursion under the specified directory voidListall (String dirName) {File file=NewFile (DirName);//instantiating a File object if(File.isdirectory ())//If the file object is an object{File f[]= File.listfiles ();//Remove all files from this folder if(f!=NULL) { for(inti=0;i<f.length;i++) {//to process each element of a file arrayListall (F[i].tostring ());//recursively process the file } } }Else //output file information directly if it is not a folderSystem.out.println (file); } //get all filenames named under a suffix in the specified directory · voidGettypepart (String dirname,string flag) {File file=NewFile (DirName);//instantiating a File object if(File.isdirectory ())//If the file object is an object{File f[]= File.listfiles ();//Remove all files from this folder if(f!=NULL) { for(inti=0;i<f.length;i++) {//to process each element of a file arrayListall (F[i].tostring ());//recursively process the file } } } Else if(File.getname (). EndsWith (flag)) {//output file information directly if it is not a folderSystem.out.println (file); } } //strline.indexof ("Hello", id)! =-1//fuzzy search for specified files under specified directory voidsearchfile (String dirname,string key) {File file=NewFile (DirName);//instantiating a File object if(File.isdirectory ())//If the file object is an object{File f[]= File.listfiles ();//Remove all files from this folder if(f!=NULL) { for(inti=0;i<f.length;i++) {//to process each element of a file arrayListall (F[i].tostring ());//recursively process the file } } } Else if(File.getname (). IndexOf (key,0)!=-1) {//output file information directly if it is not a folderSystem.out.println (file); } } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub }}
Some actions about folders