Java執行CMD並獲取執行結果

public static List<String> execWithResult(String cmd){
        List<String> resultList = new ArrayList<String>();
        
        if(StringUtils.isEmpty(cmd)) {
            return resultList;
        }
        
        Process process = null;
        BufferedReader bufrIn = null;
        
        try {
            process = Runtime.getRuntime().exec(cmd);
            process.waitFor();
            bufrIn = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
             String line;
               while ((line = bufrIn.readLine()) != null) {
                   resultList.add(line);
               }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(bufrIn != null) {
                try {
                    bufrIn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        return resultList;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章