/** * Copy 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 * @param oldpath String original file path such as: C:/FQF * @param newpath string after copy path such as: f:/ FQF/FF * @return Boolean*/ 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[1024x768*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 (); } }
Ps:
Copy the files in the assets directory
InputStream is = Ctx.getassets (). Open ("test.apk");
If possible, do not use Try/catch, check file attributes and other parameters when copying files, ensure foolproof
if (!oldfile.exists ()) {return;} if (!oldfile.isfile ()) {return;}
"Android" Copy files to another directory