Copy all the contents of a folder to a specified location
/** * Copy all Files under folder to the specified directory * @param oldpath * @param newpath * * * public static void CopyFolder (String oldpath, Stri ng NewPath) {try {///If the folder does not exist, create a new folder (NewPath). Mkdirs ();//Read the contents of the entire folder into an array of File strings, set a cursor I below, Move down and start reading this array file FileList = new file (OldPath); string[] File = Filelist.list ();//To note that this temp is just a temporary file pointer//whole program and does not create temporary file temp = null;for (int i = 0; i < file.length; i++) {///if OldPath with the path delimiter/or \ End, then the oldpath/file name can be//otherwise you have to OldPath after the path delimiter and add the file name//Who knows you pass the parameter is f:/a or f:/a/ah? if (Oldpath.endswith (file.separator)) {temp = new file (OldPath + file[i]);} else {temp = new file (OldPath + file.separator + File[i]);} If the cursor encounters a file if (Temp.isfile ()) {FileInputStream input = new FileInputStream (temp); FileOutputStream output = new FileOutputStream (newpath+ "/" + "rename_" + (Temp.getname ()). ToString ()); byte[] Bufferarray = new byte[1024 * 64];int prereadlength;while ((prereadlength = Input.read (bufferarray))! =-1) {Output.write ( Bufferarray, 0, prereadlength);} Output.flush (); output.close(); Input.close ();} If the cursor encounters a folder if (Temp.isdirectory ()) {CopyFolder (OldPath + "/" + file[i], NewPath + "/" + File[i]);}}} catch (Exception e) {System.out.println ("error copying entire folder contents operation");}}
Copy all the contents of a folder to a specified location