Delete a folder
ImportJava.io.File; Public classDeletedir {/** * @paramargs*/ Public Static voidMain (string[] args) {File Filedir=NewFile ("D:\\360down"); Deletedirect (Filedir); System.out.println ("Over"); } /** * @paramfile*/ Private Static voidDeletedirect (File filedir) {//If the directory if(Filedir.exists () &&filedir.isdirectory ()) {file[] listfiles=Filedir.listfiles (); for(file file:listfiles) {deletedirect (file); } filedir.delete (); } Else{filedir.delete (); } }}
Import java.io.*; /** * Copy folder or folder */public class CopyDirectory {//source folder static String Url1 = "F:/photos"; Destination folder static String Url2 = "D:/tempphotos"; public static void Main (String args[]) throws IOException {//Create destination folder (new File (URL2)). Mkdirs (); Gets the file or directory under which the source folder is currently file[] File = (new file (URL1)). Listfiles (); for (int i = 0; i < file.length; i++) {if (File[i].isfile ()) {//Copy files CopyFile (file[i],new file (Url2+file[i].getname ())); } if (File[i].isdirectory ()) {//copy directory String Sourcedir=url1+file.separat Or+file[i].getname (); String Targetdir=url2+file.separator+file[i].getname (); Copydirectiory (SourceDir, TargetDir); }}}//Copy file public static void CopyFile (file Sourcefile,file targetfile) throws ioexception{ NewThe file input stream is built and buffered fileinputstream input = new FileInputStream (sourcefile); Bufferedinputstream inbuff=new Bufferedinputstream (input); Creates a new file output stream and buffers it fileoutputstream output = new FileOutputStream (targetfile); Bufferedoutputstream outbuff=new bufferedoutputstream (output); Buffer array byte[] b = New byte[1024 * 5]; int Len; while (Len =inbuff.read (b))! =-1) {outbuff.write (b, 0, Len); }//Refresh this buffered output stream Outbuff.flush (); Close flow inbuff.close (); Outbuff.close (); Output.close (); Input.close (); }//Copy folder public static void Copydirectiory (String sourcedir, String targetDir) throws Ioexcepti on {//New target directory (new File (TargetDir)). Mkdirs (); Gets the file or directory under which the source folder is currently file[] File = (new file (SourceDir)). Listfiles (); for (int i = 0; i < filE.length; i++) {if (File[i].isfile ()) {//source file Sourcefile=file[i]; The target file is targetfile=new files (new file (TargetDir). GetAbsolutePath () +file.separator+file[i ].getname ()); CopyFile (Sourcefile,targetfile); if (File[i].isdirectory ()) {//Prepare the copied source folder String Dir1=sourcedir + "/" + File[i].getname (); Prepare the destination folder for replication String Dir2=targetdir + "/" + file[i].getname (); Copydirectiory (Dir1, DIR2); }}}} import java.io.*;/** * Copy folder or folder */public class CopyDirectory {//source folder static String Url1 = "F:/photos"; Destination folder static String Url2 = "D:/tempphotos"; public static void Main (String args[]) throws IOException {//Create destination folder (new File (URL2)). Mkdirs (); Gets the file or directory under which the source folder is currently under file[] File = (new file (URL1)).Listfiles (); for (int i = 0; i < file.length; i++) {if (File[i].isfile ()) {//Copy file Copyf Ile (file[i],new file (Url2+file[i].getname ())); } if (File[i].isdirectory ()) {//copy directory String Sourcedir=url1+file.separator+file [I].getname (); String Targetdir=url2+file.separator+file[i].getname (); Copydirectiory (SourceDir, TargetDir); }}}//copy file public static void CopyFile (file Sourcefile,file targetfile) throws ioexception{//new file input stream and Buffer it fileinputstream input = new FileInputStream (sourcefile); Bufferedinputstream inbuff=new Bufferedinputstream (input); Creates a new file output stream and buffers it fileoutputstream output = new FileOutputStream (targetfile); Bufferedoutputstream outbuff=new bufferedoutputstream (output); Buffer array byte[] b = New byte[1024 * 5]; int Len; while (Len =inbuff.Read (b))! =-1) {outbuff.write (b, 0, Len); }//Refresh this buffered output stream Outbuff.flush (); Close flow inbuff.close (); Outbuff.close (); Output.close (); Input.close (); }//Copy folder public static void Copydirectiory (String sourcedir, String targetDir) throws IOException { New destination directory (new File (TargetDir)). Mkdirs (); Gets the file or directory under which the source folder is currently file[] File = (new file (SourceDir)). Listfiles (); for (int i = 0; i < file.length; i++) {if (File[i].isfile ()) {//source file s Ourcefile=file[i]; The target file is targetfile=new files (new file (TargetDir). GetAbsolutePath () +file.separator+file[i].getname ()); CopyFile (Sourcefile,targetfile); if (File[i].isdirectory ()) {//Prepare the copied source folder String Dir1=sourcedir + "/" + File[i ].getname (); Prepare the destination folder for replication String Dir2=targetdir + "/" + file[i].getname (); Copydirectiory (Dir1, DIR2); } } }}
Java Delete folder Java implementation copy file or folder