当前位置:编程学习 > JAVA >>

java 中读取process数据

我在java中通过Process proc=Runtime.getRuntime().exec(cmd);   调用了其他程序,在这个进程中,有我想要的数据,但是我通过BufferedInputStream isb=new BufferedInputStream(proc.getInputStream());
FileOutputStream os=new FileOutputStream(savefile);
读出来的是0kb,不知道如何读出来其中有用的内容,请指教,谢谢了。 --------------------编程问答--------------------

Process p = Runtime.getRuntime().exec("ipconfig");
BufferedInputStream is = new BufferedInputStream(p.getInputStream());
byte[] buf = new byte[1024];
int size = 0;
while((size = is.read(buf)) != -1)
System.out.println(new String(buf,0,size,"gbk"));
is.close();


没有问题,这样读取屏幕输出,你执行的是什么程序? --------------------编程问答-------------------- 不行读取时null --------------------编程问答-------------------- Process p = Runtime.getRuntime().exec("ipconfig");
p.waitFor(); //需要加这行。

不过,我这运行到waitFor()就不走了。 --------------------编程问答-------------------- 2楼的 正确 我专门测试了下 --------------------编程问答-------------------- 加waitfor干嘛..
一般程序直接拿输出就行了

你确定是把我的代码贴回去跑过了?
你的代码哪?你运行的啥东西?你确定那程序有输出? --------------------编程问答-------------------- try {   
String exeFullPathName="C:\\Program Files\\Macromedia\\FlashPaper 2\\FlashPrinter.exe";   
String message="C:\\1.jpg";   
String  saveFilePathName="C:\\Program Files\\Macromedia\\FlashPaper 2\\Interface";
String print="flashpaperprinterui2";
File savefile=new File(saveFilePathName,"1.swf");
byte b[]=new byte[400];
String []cmd={exeFullPathName,message};   
Process proc=Runtime.getRuntime().exec(cmd);   

if(proc.getErrorStream()!=null){
BufferedInputStream isb=new BufferedInputStream(proc.getInputStream());
FileOutputStream os=new FileOutputStream(savefile);
int length=0;proc.waitFor();
while((length=isb.read(b))!=-1){
os.write(b,0,length);

}

isb.close();
os.close();
}
} catch (IOException e) {   
// TODO Auto-generated catch block   
e.printStackTrace();   
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}   
不好意识 ,我没说清上边是源码。 --------------------编程问答-------------------- 这是GUI程序吧?
InputStream只能拿到标准输出流,GUI肯定是做不到的
--------------------编程问答-------------------- 1L正解
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,