国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

使用ProcessBuilder調用外部命令,并返回大量結果

瀏覽:100日期:2024-07-19 08:19:14
內容: 在網上常見的用Java調用外部命令返回結果的方法是: process =runtime.exec(cmd) is = process.getInputStream(); isr=new InputStreamReader(is); br =new BufferedReader(isr); while( (line = br.readLine()) != null ) { out.println(line); out.flush(); } 這種方法在遇到像cvs checkout modules這樣不能馬上返回結果的命令來說是無效的,不僅得不到返回結果,進程也會終止。其原因是,process在沒有來得及gegtInputStream是,調用了BufferedReader.readLine其返回結果是null,也就是說循環一開始就會停止。因此想要正常運行只能直接讀取process.getInputStream(),如下:import java.io.*;/** * * @author tyrone * */public class CMDExecute { /** * @param cmd * @return * @throws IOException */ public synchronized String run(String[] cmd,String workdirectory) throws IOException{ String line=null; String result=''; try { ProcessBuilder builder = new ProcessBuilder(cmd); //set working directory if (workdirectory!=null) builder.directory(new File(workdirectory)); builder.redirectErrorStream(true); Process process = builder.start(); InputStream in=process.getInputStream(); byte[] re=new byte[1024]; while (in.read(re)!= -1) { System.out.println(new String(re)); result = result + new String(re); } in.close(); } catch (Exception ex) { ex.printStackTrace(); } return result; } /** * @param args=cvs log */ public static void main(String[] args){ String result=null; CMDExecute cmdexe=new CMDExecute(); try { result= cmdexe.run(args,'D:MyProjectcolimasaxis_c'); System.out.println(result); }catch ( IOException ex ){ ex.printStackTrace(); } }}經過測試,本方法可以運行返回大量結果的應用程序。我的blog:http://blog.csdn.net/tyrone1979 Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 在網上常見的用Java調用外部命令返回結果的方法是: process =runtime
相關文章:
主站蜘蛛池模板: 望江县| 罗江县| 南靖县| 临江市| 万荣县| 图们市| 长丰县| 封丘县| 河西区| 灌阳县| 左云县| 武城县| 任丘市| 台江县| 巩义市| 定西市| 安化县| 区。| 木里| 昭觉县| 城口县| 博罗县| 裕民县| 阳春市| 北海市| 曲阳县| 永清县| 上饶县| 瑞金市| 澎湖县| 西华县| 丹江口市| 从江县| 武城县| 固安县| 三门峡市| 秭归县| 汝州市| 武平县| 叶城县| 淳安县|