概述
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Test1 {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("cmd"); //cmd /c start 可以打开另一个窗口
PrintWriter writer = new PrintWriter(process.getOutputStream());
new CommandThread(writer).start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
Thread.currentThread().interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
static class CommandThread extends Thread{
PrintWriter writer;
BufferedReader br = null;
CommandThread(PrintWriter writer){
this.writer = writer;
br = new BufferedReader(new InputStreamReader(System.in));
this.setDaemon(true);
}
@Override
public void run() {
try {
String cmd = null;
while((cmd = br.readLine()) != null){
writer.println(cmd);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后
以上就是苗条小海豚为你收集整理的java与cmd命令窗口交互的全部内容,希望文章能够帮你解决java与cmd命令窗口交互所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复