- try {
- Process process = Runtime.getRuntime().exec("cmd.exe");
- OutputStream outputStream = process.getOutputStream();
- final InputStream inputStream = process.getInputStream();
- new Thread(new Runnable(){
- byte[] cache = new byte[1024];
- public void run() {
- System.out.println("listener started");
- try {
- while(inputStream.read(cache)!=-1){
- System.out.println(new String(cache));
- }
- } catch (IOException e) {
- //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- }).start();
- outputStream.write(new byte[]{'d','i','r','/n'});
- //inputStream.
- int i = process.waitFor();
- System.out.println("i=" + i);
- } catch (Exception e) {
- e.printStackTrace();
- }
try { Process process = Runtime.getRuntime().exec("cmd.exe"); OutputStream outputStream = process.getOutputStream(); final InputStream inputStream = process.getInputStream(); new Thread(new Runnable(){ byte[] cache = new byte[1024]; public void run() { System.out.println("listener started"); try { while(inputStream.read(cache)!=-1){ System.out.println(new String(cache)); } } catch (IOException e) { //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }).start(); outputStream.write(new byte[]{'d','i','r','/n'}); //inputStream. int i = process.waitFor(); System.out.println("i=" + i); } catch (Exception e) { e.printStackTrace(); }
明白了没?
ggmmaallee (初级程序员) 2008-06-02
- try {
- Process process = Runtime.getRuntime().exec("cmd.exe");
- final BufferedWriter outputStream = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
- final BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
- new Thread(new Runnable() {
- String line;
- public void run() {
- System.out.println("listener started");
- try {
- while ((line=inputStream.readLine()) != null) {
- System.out.println(line);
- }
- } catch (IOException e) {
- //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- }).start();
- new Thread(new Runnable() {
- final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- public void run() {
- System.out.println("writer started");
- String line;
- try {
- while ((line = br.readLine()) != null) {
- outputStream.write(line + "/r/n");
- outputStream.flush();
- }
- } catch (IOException e) {
- //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- }).start();
- int i = process.waitFor();
- System.out.println("i=" + i);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }