1. Copying folders
Public Static voidCopydir (String OldPath, String NewPath)throwsIOException {File file=NewFile (OldPath); String[] FilePath=file.list (); if(! (NewFile (NewPath)). Exists ()) {(NewFile (NewPath)). mkdir (); } for(inti = 0; i < filepath.length; i++) { if((NewFile (OldPath + "//" +Filepath[i]). Isdirectory ()) {//file Newdir = new file (path + "//"+ filepath[i]); //if (!newdir.exists ()) {//Newdir.mkdir ();// }Copydir (OldPath + "//" + filepath[i], NewPath + "//" +Filepath[i]); } if(NewFile (OldPath + "//" +Filepath[i]). Isfile ()) {CopyFile (OldPath+ "//" + filepath[i], NewPath + "//" +Filepath[i]); } } }
2. How to copy files
Public Static voidCopyFile (String OldPath, String NewPath)throwsIOException {File oldFile=NewFile (OldPath); File File=NewFile (NewPath); FileInputStream in=NewFileInputStream (OldFile); FileOutputStream out=Newfileoutputstream (file);; byte[] buffer=New byte[2097152]; while((in.read (buffer))! =-1) {out.write (buffer); } }
3. Calling methods
Public Static voidMain (string[] args)throwsIOException {Scanner sc=NewScanner (system.in); System.out.println ("Please enter the source directory:"); String SourcePath=Sc.nextline (); System.out.println ("Please enter a new directory:"); String Path=Sc.nextline (); //String SourcePath = "D://AA "; //String Path = "D://BB ";copydir (SourcePath, path); }
Java copies all files and folders in a folder to another folder