如何使这个工作在Windows上,文件filename.txt没有被创建。运行时问题
Process p = Runtime.getRuntime().exec("cmd echo name > filename.txt");
显然,期望输出是一个 “FILENAME.TXT” 应被创建(C:\ Documents和Settings \用户名\ FILENAME.TXT)与内容 “名称”。
之所以能够用下面的代码来管理,即使该文件是 “FILENAME.TXT” 不被用的ProcessBuilder
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c cleartool lsview");
// Directly to file
//Process p = Runtime.getRuntime().exec(
// new String[] { "cmd", "/c", "cleartool lsview > filename.txt" },null, new File("C:/Documents and Settings/username/"));
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
或使用ProceessBuilder创建,
Process process = new ProcessBuilder("cmd", "/c", "cleartool lsview").start();
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.printf("Output of running %s is:", Arrays.toString(args));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
怎么不工作吗? – Bozho 2010-09-23 11:06:23
它不起作用的方式? – 2010-09-23 11:06:45
显然?你是从'C:\ Documents and Settings \ username \'文件夹直接运行它吗? – 2010-09-23 11:12:33