Copy all the contents of a folder to a specified location

Source: Internet
Author: User

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

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.