1

i am using below code to copy multiple files from one folder to another , but it takes too much time , it is requested is their any best practice to implement to increase speed. i will be grateful.( Note: i am not moving files )

void copyFile(File sourceLocation, File targtLocation) throws IOException { if (sourceLocation.exists()) { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(sourceLocation); new File(String.valueOf(targtLocation)).delete(); out = new FileOutputStream(targtLocation); } catch (FileNotFoundException e) { e.printStackTrace(); } // Copy the bits from instream to outstream byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); sourceLocation.delete(); Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); scanIntent.setData(Uri.fromFile(sourceLocation)); sendBroadcast(scanIntent); Log.e("debug", "Copy file successful."); } else { Log.v("debug", "Copy file failed. Source file missing."); } } 
2

2 Answers 2

2

At last did it. when i use BufferedOutputStream and BufferedInputStream the processing time was half. Unlike input/outputstream, it do not call the underlying system for each byte read/write. instead it call once and buffer those streams .

void copyFileFast1(File sourceLocation, File targtLocation) throws IOException { if (sourceLocation.exists()) { FileInputStream fin = null; FileOutputStream fout = null; Log.i("debug","source "+sourceLocation); Log.i("debug","des "+targtLocation); try { fin = new FileInputStream(sourceLocation); new File(String.valueOf(targtLocation)).delete(); fout = new FileOutputStream(targtLocation); } catch (FileNotFoundException e) { e.printStackTrace(); } // Copy the bits from instream to outstream byte[] buf = new byte[2048]; int len; BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fout); BufferedInputStream bufferedInputStream=new BufferedInputStream(fin); while ((len = bufferedInputStream.read(buf)) > 0) { bufferedOutputStream.write(buf, 0, len); } fin.close(); bufferedOutputStream.close(); fout.close(); sourceLocation.delete(); Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); scanIntent.setData(Uri.fromFile(sourceLocation)); sendBroadcast(scanIntent); Log.e("debug", "Copy file successful."); } else { Log.v("debug", "Copy file failed. Source file missing."); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

Use destination.transferFrom() method to do fast file transfer

 public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs(); if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } 

Or you can also use FileUtils.copyFile() method

 String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/tt_temp.3gp"; File source = new File(sourcePath); String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/tt_1A.3gp"; File destination = new File(destinationPath); try { FileUtils.copyFile(source, destination); } catch (IOException e) { e.printStackTrace(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.