Here you can find the source of copyFileByCommand(String src, String dst, boolean isMove)
Parameter | Description |
---|---|
src | a parameter |
dst | a parameter |
isMove | move or copy |
public static void copyFileByCommand(String src, String dst, boolean isMove)
//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(); } } } }