Android File Copy copyFile(String strSrc, String strDest)

Here you can find the source of copyFile(String strSrc, String strDest)

Description

Copy file from src to des, override if des exist

Parameter

Parameter Description
strSrc source file
strDest destination file

Return

true if succees, otherswise false

Declaration

// //////////////////////////////////////////////////////
public static boolean copyFile(String strSrc, String strDest) 

Method Source Code

//package com.java2s;
import java.io.*;

public class Main {
    public static final int BUFFER_SIZE = 65536;

    /**/*from  w  ww .  j  a  va2s .co m*/
     * Copy file from src to des, override if des exist
     * 
     * @param strSrc
     *            source file
     * @param strDest
     *            destination file
     * @return true if succees, otherswise false
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static boolean copyFile(String strSrc, String strDest) {
        FileInputStream isSrc = null;
        FileOutputStream osDest = null;
        try {
            File flDest = new File(strDest);
            if (flDest.exists())
                flDest.delete();

            File flSrc = new File(strSrc);
            if (!flSrc.exists())
                return false;

            isSrc = new FileInputStream(flSrc);
            osDest = new FileOutputStream(flDest);

            byte btData[] = new byte[BUFFER_SIZE];
            int iLength;
            while ((iLength = isSrc.read(btData)) != -1)
                osDest.write(btData, 0, iLength);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            safeClose(isSrc);
            safeClose(osDest);
        }
    }

    /**
     * Close object safely
     * 
     * @param is
     *            InputStream
     */
    // //////////////////////////////////////////////////////
    public static void safeClose(InputStream is) {
        try {
            if (is != null)
                is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Close object safely
     * 
     * @param os
     *            OutputStream
     */
    // //////////////////////////////////////////////////////
    public static void safeClose(OutputStream os) {
        try {
            if (os != null)
                os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Close object safely
     * 
     * @param fl
     *            RandomAccessFile
     */
    // //////////////////////////////////////////////////////
    public static void safeClose(RandomAccessFile fl) {
        try {
            if (fl != null)
                fl.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(String origPath, String destPath)
  2. copyFile(String sourceFileName, String destinationFileName)
  3. copyFile(String sourcePath, String destPath)
  4. copyFile(String sourcePathName, String destPathName)
  5. copyFile(String srcFile, String destFile)
  6. copyFileByCommand(String src, String dst, boolean isMove)
  7. copyFileLazy(String source, String destination)
  8. copyFileToFile(File file, File outFile)
  9. copyFiles(File src, File dst)