java 调用shell命令cp

来源:互联网 发布:sst nc数据下载 编辑:程序博客网 时间:2024/06/05 04:23

其他命令可以直接用string ,cp命令要用string[],原因不清楚

package com;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class Test {    public static void main(String args[]) {        Process process = null;        List<String> processList = new ArrayList<String>();        String[] command = new String[] { "sh", "-c", "/bin/cp /data1 /data2" };        StringBuffer sb = new StringBuffer();        for (int i = 0; i < command.length; i++) {            sb.append(command[i]);        }        String s = sb.toString();        System.out.println("command:" + s);        try {            process = Runtime.getRuntime().exec(command);            BufferedReader input = new BufferedReader(new InputStreamReader(                    process.getInputStream()));            String line = "";            while ((line = input.readLine()) != null) {                processList.add(line);            }            input.close();        } catch (IOException e) {            e.printStackTrace();        }        for (String line : processList) {            System.out.println(line);        }    }}
原创粉丝点击