java操作linux工具類
阿新 • • 發佈:2019-05-09
tst ade pre nds public and linux工具 buffer con
package com.example.oss.config; import java.io.BufferedReader; import java.io.InputStreamReader; public class ExcuteLinux { public static String exeCmd(String commandStr) { String result = null; try { String[] cmd = new String[]{"/bin/sh", "-c",commandStr}; Process ps= Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = br.readLine()) != null) { //執行結果加上回車 sb.append(line).append("\n"); } result= sb.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } }
java操作linux工具類