Packagetext;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.PrintStream; Public classfileCopy { Public Static voidMain (string[] args)throwsioexception {copy ("G:/test", "E:/test"); System.out.println ("Copy succeeded!"); } Private Static voidCopy (String f1, string F2)throwsIOException {File file1=NewFile (F1); /*file File2=new file (F2);*/file[] Flist=File1.listfiles (); for(File f:flist) {if(F.isfile ()) {CopyFile2 (F.getpath ()), F2+ "/" +f.getname ());//How to call a copy fileSystem.out.println ("Original Path [" +f.getpath () + "] copied path [" +f2+ "/" +f.getname () + "]"); }Else if(F.isdirectory ()) {CopyFile1 (F.getpath ()), F2+ "/" +f.getname ());//How to call a copy folderSystem.out.println ("Original Path [" +f.getpath () + "] copied path [" +f2+ "/" +f.getname () + "]"); } } } /*** Copy Folder *@throwsIOException*/ Public Static voidCopyFile1 (String f1,string F2)throwsioexception{//Create a folderFile file=NewFile (F2); if(!file.exists ()) {File.mkdirs (); } copy (F1,F2); } /*** Copy files *@throwsIOException*/ Public Static voidCopyFile2 (String f1, string F2)throwsIOException {Try{InputStream input=NewFileInputStream (F1); OutputStream Output=NewFileOutputStream (F2); byte[] bt=New byte[1024]; if((input!=NULL) && (output!=NULL)){ while((Input.read (BT))! = (-1) {output.write (BT,0, bt.length); }} input.close (); Output.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } }}
Java Replication folder and all subdirectories and files