Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java程序中运行命令行命令并将控制台的输出复制到文件中?_Java - Fatal编程技术网

如何在java程序中运行命令行命令并将控制台的输出复制到文件中?

如何在java程序中运行命令行命令并将控制台的输出复制到文件中?,java,Java,您的问题不太清楚,但如果您的代码正常运行,请将cmd/c dir替换为cmd/c dir>test.txt直接使用进程会有一些小的缺陷。看看这个例子。不过,我不确定安卓系统 class cmdln_file { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in

您的问题不太清楚,但如果您的代码正常运行,请将
cmd/c dir
替换为
cmd/c dir>test.txt

直接使用进程会有一些小的缺陷。看看这个例子。不过,我不确定安卓系统
class cmdln_file {

    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("cmd /c dir");
            //Process pr = rt.exec("C://apkfiles//new_pro2.apk");

            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

            String line=null;

            while((line=input.readLine()) != null) {
                System.out.println(line);
            }

            int exitVal = pr.waitFor();
            System.out.println("Exited with error code "+exitVal);

        } catch(Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}