Android File Copy copyFileByCommand(String src, String dst, boolean isMove)

Here you can find the source of copyFileByCommand(String src, String dst, boolean isMove)

Description

Use system command to copy file

Parameter

Parameter Description
src a parameter
dst a parameter
isMove move or copy

Declaration

public static void copyFileByCommand(String src, String dst,
        boolean isMove) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*w  w w.j a  v a2  s . c  om*/
     * Use system command to copy file
     * @param src
     * @param dst
     * @param isMove move or copy
     */
    public static void copyFileByCommand(String src, String dst,
            boolean isMove) {
        File file = new File(dst).getParentFile();
        if (!file.exists() && !file.mkdirs())
            return;

        String[] cmd = null;
        if (System.getProperty("os.name").toLowerCase()
                .startsWith("windows")) {
            cmd = new String[5];
            cmd[0] = "cmd";
            cmd[1] = "/c";
            cmd[2] = isMove ? "move" : "copy";
            cmd[3] = src;
            cmd[4] = dst;
        } else {
            cmd = new String[3];
            cmd[0] = isMove ? "mv" : "cp";
            cmd[1] = src;
            cmd[2] = dst;
        }

        InputStream in = null;
        Process process = null;
        try {
            process = new ProcessBuilder(cmd).start();
            in = process.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                process.destroy();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

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