Search code examples
androidperformanceinputstreamoutputstreamfileoutputstream

Android: best practice ? , when copying multiple files from one folder to another , it takes much time ,


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.");
    }
}

Solution

  • 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.");
            }
        }