首页 > 其他 > 详细

根据命令执行

时间:2019-07-01 11:20:19      阅读:113      评论:0      收藏:0      [点我收藏+]
/** * 判断什么操作系统 */ public String osName = System.getProperty("os.name"); /** * 根据命令执行, * @param cmdstr * @param isNeedReturn * @return list * @throws Exception */ public List<String> execute(String cmdstr, boolean isNeedReturn) throws Exception { //存储结果 List<String> lineList = new ArrayList<String>(); String[] cmdarray; if (osName.startsWith("Windows")) { cmdarray = new String[]{"cmd", "/c", cmdstr}; } else { cmdarray = new String[]{"/bin/bash", "-c", cmdstr}; } //执行命令 Process process = Runtime.getRuntime().exec(cmdarray); if (isNeedReturn) { //获取结果流 InputStream fis = process.getInputStream(); //读取结果流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { if (line.trim().length() != 0) { lineList.add(line); } } return lineList; } return null; }

根据命令执行

原文:https://blog.51cto.com/357712148/2415700

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!