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

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

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

瀏覽:76日期: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
相關文章:
主站蜘蛛池模板: 隆德县| 同江市| 翼城县| 长治县| 肥乡县| 洪湖市| 西峡县| 永靖县| 伽师县| 金溪县| 临安市| 大庆市| 大兴区| 澄江县| 海淀区| 綦江县| 兴安县| 邻水| 舞钢市| 将乐县| 鄄城县| 贵德县| 昌乐县| 吉隆县| 视频| 屯留县| 乌鲁木齐市| 东山县| 赤壁市| 平山县| 湖口县| 景洪市| 新晃| 鄂托克前旗| 靖宇县| 翁牛特旗| 印江| 贡山| 武邑县| 瑞昌市| 鲁山县|