** *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