zoukankan      html  css  js  c++  java
  • java执行cmd命令和linux命令

    文章出处http://blog.csdn.net/xh16319/article/details/17302947

    一:window下执行cmd指定

    一:window下执行cmd指定
    程序例子:
    [java] view plain copy
    /*该方法实现文件自动复制功能。利用系统命令将指定文件名从源路径复制到目的路径 
         * 如果目的路径不存在时,自动创建目的路径 
         * */   
    public static boolean copyFile(String origpath, String destpath, String filename) throws Exception{  
        String osName = System.getProperty("os.name");  
        boolean flag = false;  
        /*系统命令支持的操作系统Windows XP, 2000 2003 7*/  
        if(!(osName.equalsIgnoreCase("windows XP") || osName.equalsIgnoreCase("windows 2000") || osName.equalsIgnoreCase("windows 2003") || osName.equalsIgnoreCase("windows 7"))){  
            return flag;  
        }  
        Runtime rt = Runtime.getRuntime();  
        Process p = null;  
        File f = new File(destpath);  
        if(!f.exists()){  
            f.mkdirs();  
        }  
        int exitVal;  
        p = rt.exec("cmd exe /c copy " + origpath+filename+" "+destpath);  
        // 进程的出口值。根据惯例,0 表示正常终止。   
        exitVal = p.waitFor();  
        if(exitVal == 0){  
            flag = true;  
        }else{  
            flag = false;  
        }  
        return flag;      
          
    }  
      
        public static void main(String[] args) {  
      
            try {  
                copyFile("D:\DATA\", "D:\a\", "131204.txt");  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
       
               
        }  
    
    二:linux下执行shell命令
    程序例子:
    [java] view plain copy
    package edu.test;  
    import java.io.InputStreamReader;  
    import java.io.LineNumberReader;  
           
        /** 
         * java在linux环境下执行linux命令,然后返回命令返回值。 
         * @author lee 
         */  
        public class ExecLinuxCMD {  
           
            public static Object exec(String cmd) {  
                try {  
                    String[] cmdA = { "/bin/sh", "-c", cmd };  
                    Process process = Runtime.getRuntime().exec(cmdA);  
                    LineNumberReader br = new LineNumberReader(new InputStreamReader(  
                            process.getInputStream()));  
                    StringBuffer sb = new StringBuffer();  
                    String line;  
                 while ((line = br.readLine()) != null) {  
                        System.out.println(line);  
                        sb.append(line).append("
    ");  
                    }  
                    return sb.toString();  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                return null;  
            }  
           
            public static void main(String[] args) {  
                // TODO Auto-generated method stub  
                String pwdString = exec("pwd").toString();  
                String netsString = exec("netstat -nat|grep -i "80"|wc -l").toString();  
                   
                System.out.println("==========获得值=============");  
                System.out.println(pwdString);  
                System.out.println(netsString);  
            }  
           
        }  
    执行结果:

     

    Java调用命令行并获取执行结果

    import java.io.BufferedReader;  
    import java.io.InputStreamReader;  
      
    public class Command {  
        public static void exeCmd(String commandStr) {  
            BufferedReader br = null;  
            try {  
                Process p = Runtime.getRuntime().exec(commandStr);  
                br = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                String line = null;  
                StringBuilder sb = new StringBuilder();  
                while ((line = br.readLine()) != null) {  
                    sb.append(line + "
    ");  
                }  
                System.out.println(sb.toString());  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
            finally  
            {  
                if (br != null)  
                {  
                    try {  
                        br.close();  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
      
        public static void main(String[] args) {  
            String commandStr = "ping www.taobao.com";  
            //String commandStr = "ipconfig";  
            Command.exeCmd(commandStr);  
        }  
    }  
  • 相关阅读:
    数据结构实践——败者树归并模拟
    systemctl介绍
    Andriod Atom x86模拟器启动报错。
    (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
    OpenCV2.3.1在CentOS6.5下的安装
    本学期课程教学要解决这个问题要点备忘录
    firefox浏览器和IE
    [LeetCode] Validate Binary Search Tree
    android性能优化优秀文章
    如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片
  • 原文地址:https://www.cnblogs.com/111testing/p/7781578.html
Copyright © 2011-2022 走看看