/**
*
*/
package pkg;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/**
* @author 2172980000502
*
*/
public class TestProcess { /**
* @param args
*/
public static void main(String[] args) {
try {
Process exec = Runtime.getRuntime().exec("cmd");
InputStream inputStream = exec.getInputStream();
OutputStream outputStream = exec.getOutputStream();
outputStream.write("dir\r\n".getBytes());
int c;
while ((c = inputStream.read())!=-1) {
System.out.print((char)c);
}
System.out.println("end");
} catch (IOException e) {
e.printStackTrace();
} }}
程序会堵塞在while 那里是什么问题呢?
还有dir命令貌似也没有执行?
*
*/
package pkg;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/**
* @author 2172980000502
*
*/
public class TestProcess { /**
* @param args
*/
public static void main(String[] args) {
try {
Process exec = Runtime.getRuntime().exec("cmd");
InputStream inputStream = exec.getInputStream();
OutputStream outputStream = exec.getOutputStream();
outputStream.write("dir\r\n".getBytes());
int c;
while ((c = inputStream.read())!=-1) {
System.out.print((char)c);
}
System.out.println("end");
} catch (IOException e) {
e.printStackTrace();
} }}
程序会堵塞在while 那里是什么问题呢?
还有dir命令貌似也没有执行?
2.一次应该只能一个命令,dir没被执行 try {
Process proc = Runtime.getRuntime().exec("ping www.baidu.com");
InputStream inputStream = proc.getInputStream();
String line = "";
BufferedReader buf = null;
buf = new BufferedReader(new InputStreamReader(inputStream));
while ((line = buf.readLine()) != null){
System.out.println(line);
}
System.out.println("end");
} catch (IOException e) {
e.printStackTrace();
}