簡體   English   中英

如何在Java程序中運行命令行命令並將控制台的輸出復制到文件中?

[英]how to run command line commands inside java program and copy the output of console into a file?

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

您的問題不太清楚,但是如果您的代碼正常運行,請用cmd /c dir > test.txt替換cmd /c dir

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM