Android File Copy copyFile(String sourcePath, String destPath)

Here you can find the source of copyFile(String sourcePath, String destPath)

Description

Copies one file to another.

License

Open Source License

Parameter

Parameter Description
sourceFile a parameter
destFile a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(String sourcePath, String destPath)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**/*  ww w  .  j  a  va2s.com*/
     * Copies one file to another.
     * From: http://stackoverflow.com/a/115086/352456
     * @param sourceFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFile(String sourcePath, String destPath)
            throws IOException {
        File sourceFile = new File(sourcePath);
        File destFile = new File(destPath);

        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();
            }
        }
    }
}

Related

  1. copyFile(String from, String target)
  2. copyFile(String from, String to)
  3. copyFile(String oldPath, String newPath)
  4. copyFile(String origPath, String destPath)
  5. copyFile(String sourceFileName, String destinationFileName)
  6. copyFile(String sourcePathName, String destPathName)
  7. copyFile(String srcFile, String destFile)
  8. copyFile(String strSrc, String strDest)
  9. copyFileByCommand(String src, String dst, boolean isMove)