我正在尝试使用一些简单的命令为 windows 命令提示符制作 Java GUI。我在额外输入方面遇到了一些问题。我可以运行“dir”命令,但是当我运行“del”命令时,我需要确认,但我似乎无法让它打印出确认消息。
public static void main(String args[])
{
try {
Process p = Runtime.getRuntime().exec("cmd /c dir");
read(p);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void read(Process p)
{
try{
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
}
catch(IOException e1) { e1.printStackTrace(); }
}
输出:
Volume in drive E has no label.
Volume Serial Number is E9ED-7E32
Directory of E:\Code\workspace\Project
04/11/2012 11:07 PM <DIR> .
04/11/2012 11:07 PM <DIR> ..
04/11/2012 09:53 PM 301 .classpath
04/11/2012 09:53 PM 383 .project
04/11/2012 09:53 PM <DIR> .settings
04/12/2012 12:09 AM <DIR> bin
04/12/2012 12:09 AM <DIR> src
04/11/2012 11:07 PM <DIR> test
2 File(s) 684 bytes
6 Dir(s) 429,106,937,856 bytes free
但是运行它会导致它挂在line=input.readLine()
public static void main(String args[])
{
try {
Process p = Runtime.getRuntime().exec("cmd /c del test");
read(p);
OutputStream oStream = p.getOutpuStream();
BufferedWriter sWriter = new BufferedWriter(newOutputStreamWriter(oStream));
sWriter.write("y");
sWriter.newLine();
oStream.close();
read(p);
} catch (IOException e) {
e.printStackTrace();
}
}
如何防止这种挂起?我还担心没有正确发送确认“y”。我觉得应该读取输出并写入输入,但这是我在多个在线站点找到的方式。任何帮助,将不胜感激。