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