Java Delete folder Java implementation copy file or folder

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.