zoukankan      html  css  js  c++  java
  • 执行命令工具类

    package common;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    /***
     * create by zipon 2018-09-10
     */
    
    public class ExecCmdUtils {
    
        /***
         * 根据系统执行命令,只需要传入命令,返回所有命令返回的信息
         * @param cmd
         * @return
         * @throws IOException
         */
        public static String execCmd(String cmd) throws IOException {
            String os = System.getProperty("os.name");
            String[] preparecmd = {};
            if(os!=null && os.toLowerCase().indexOf("windows")>=0) {
                preparecmd = new String[]{"cmd", "/C", cmd};
            }else if (os!=null && os.toLowerCase().indexOf("linux")>=0){
                preparecmd = new String[]{"/bin/sh", "-c", cmd};
            }else {
                return null;
            }
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec(preparecmd);
            //取得命令结果的输出流
            InputStream fis=p.getInputStream();
            //用一个读输出流类去读
            InputStreamReader isr=new InputStreamReader(fis,"GBK");
            //用缓冲器读行
            BufferedReader br=new BufferedReader(isr);
            String line=null;
            String result = "";
            //直到读完为止
            while((line=br.readLine())!=null)
            {
                result = result+line+"
    ";
            }
            //去掉最后一个换行符
            result = result.substring(0,result.length()-2);
            System.out.println("执行结果:
    "+result);
            return result;
        }
    }
  • 相关阅读:
    transform 多值先后执行顺序
    css 清除浮动
    鼠标移动到图片,图片放大
    js 事件循环机制EventLoop
    web安全
    web前端踩坑记录
    滚动加载图片(懒加载)
    css 兼容问题
    模块化 AMD与CMD 规范
    css 盒子模型 ie盒子模型
  • 原文地址:https://www.cnblogs.com/zipon/p/9617958.html
Copyright © 2011-2022 走看看