Java copy files from one directory to another

Source: Internet
Author: User

** *copying a single file* @param oldpath String original file path such as: c:/Fqf.txt* @param NewPath String After copying the path such as: f:/Fqf.txt* @return Boolean*/ Public voidcopyFile (String OldPath, String newpath) {Try { intBytesum = 0; intByteread = 0; File Oldfile=NewFile (oldpath);if(Oldfile.exists ()) {//when a file existsInputStream instream =NewFileInputStream (OldPath);//read in the original fileFileOutputStream fs =NewFileOutputStream (NewPath);byte[] buffer =New byte[1444]; intlength; while((Byteread = instream.read (buffer))! =-1) {bytesum+ = Byteread;//number of bytes file sizeSystem.out.println (bytesum); fs.write (buffer,0, Byteread); } instream.close (); } } Catch(Exception e) {System.out.println ("Error copying Single file operation"); E.printstacktrace (); } } /*** Copy entire folder contents *@paramOldPath String Original file path such as: C:/FQF *@paramnewpath String Copy after path such as: F:/fqf/ff *@returnBoolean*/  Public voidCopyFolder (String OldPath, String newpath) {Try { (NewFile (NewPath)). Mkdirs ();//Create a new folder if the folder does not existFile a=NewFile (OldPath); string[] File=a.list (); File Temp=NULL;  for(inti = 0; i < file.length; i++) { if(Oldpath.endswith (file.separator)) {temp=NewFile (oldpath+File[i]); } Else{Temp=NewFile (oldpath+file.separator+File[i]); } if(Temp.isfile ()) {FileInputStream input=NewFileInputStream (temp); FileOutputStream Output=NewFileOutputStream (NewPath + "/" +(Temp.getname ()). ToString ()); byte[] B =New byte[1024 * 5]; intLen; while(len = Input.read (b))! =-1) {Output.write (b,0, Len); } output.flush (); Output.close (); Input.close (); } if(Temp.isdirectory ()) {//if it is a sub-folderCopyFolder (oldpath+ "/" +file[i],newpath+ "/" +File[i]); } } } Catch(Exception e) {System.out.println ("Error copying entire folder contents operation"); E.printstacktrace (); } }

Java copies files from one directory to another

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.