java执行cmd命令和linux命令

来源:互联网 发布:python print函数 编辑:程序博客网 时间:2024/06/06 09:16

一:window下执行cmd指定

程序例子:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*该方法实现文件自动复制功能。利用系统命令将指定文件名从源路径复制到目的路径 
  2.      * 如果目的路径不存在时,自动创建目的路径 
  3.      * */   
  4. public static boolean copyFile(String origpath, String destpath, String filename) throws Exception{  
  5.     String osName = System.getProperty("os.name");  
  6.     boolean flag = false;  
  7.     /*系统命令支持的操作系统Windows XP, 2000 2003 7*/  
  8.     if(!(osName.equalsIgnoreCase("windows XP") || osName.equalsIgnoreCase("windows 2000") || osName.equalsIgnoreCase("windows 2003") || osName.equalsIgnoreCase("windows 7"))){  
  9.         return flag;  
  10.     }  
  11.     Runtime rt = Runtime.getRuntime();  
  12.     Process p = null;  
  13.     File f = new File(destpath);  
  14.     if(!f.exists()){  
  15.         f.mkdirs();  
  16.     }  
  17.     int exitVal;  
  18.     p = rt.exec("cmd exe /c copy " + origpath+filename+" "+destpath);  
  19.     // 进程的出口值。根据惯例,0 表示正常终止。   
  20.     exitVal = p.waitFor();  
  21.     if(exitVal == 0){  
  22.         flag = true;  
  23.     }else{  
  24.         flag = false;  
  25.     }  
  26.     return flag;      
  27.       
  28. }  
  29.   
  30.     public static void main(String[] args) {  
  31.   
  32.         try {  
  33.             copyFile("D:\\DATA\\", "D:\\a\\", "131204.txt");  
  34.         } catch (Exception e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         }  
  38.    
  39.            
  40.     }  


二:linux下执行shell命令

程序例子:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package edu.test;  
  2. import java.io.InputStreamReader;  
  3. import java.io.LineNumberReader;  
  4.        
  5.     /** 
  6.      * java在linux环境下执行linux命令,然后返回命令返回值。 
  7.      * @author lee 
  8.      */  
  9.     public class ExecLinuxCMD {  
  10.        
  11.         public static Object exec(String cmd) {  
  12.             try {  
  13.                 String[] cmdA = { "/bin/sh""-c", cmd };  
  14.                 Process process = Runtime.getRuntime().exec(cmdA);  
  15.                 LineNumberReader br = new LineNumberReader(new InputStreamReader(  
  16.                         process.getInputStream()));  
  17.                 StringBuffer sb = new StringBuffer();  
  18.                 String line;  
  19.              while ((line = br.readLine()) != null) {  
  20.                     System.out.println(line);  
  21.                     sb.append(line).append("\n");  
  22.                 }  
  23.                 return sb.toString();  
  24.             } catch (Exception e) {  
  25.                 e.printStackTrace();  
  26.             }  
  27.             return null;  
  28.         }  
  29.        
  30.         public static void main(String[] args) {  
  31.             // TODO Auto-generated method stub  
  32.             String pwdString = exec("pwd").toString();  
  33.             String netsString = exec("netstat -nat|grep -i \"80\"|wc -l").toString();  
  34.                
  35.             System.out.println("==========获得值=============");  
  36.             System.out.println(pwdString);  
  37.             System.out.println(netsString);  
  38.         }  
  39.        
  40.     }  
执行结果:



0 0