Java中执行Shell,window批量处理 .

64 篇文章 0 订阅
/*
 *  jShell.java
 *  class jShell is used for executing shell command
 *  USAGE:
 *      jShell obj=new jShell(shellCommand);
 *      obj.startErr();
 *      obj.startOut();
 *      obj.startIn();
 *  You can Interupt I/O thread when nessasary:
 *      obj.interruptErr();
 *      obj.interruptOut();
 *      obj.interruptIn();
 *  
 *  BY Ahui Wang    Nankai U.    2007-05-12   
 */
 import java.io.*;
   public class jShell {
    Thread tIn;    //handle input of child process 27.    Thread tOut;//handle output of child process 28.    Thread tErr;//handle error output of child process 29.    
    public jShell(String shellCommand){
        
        Process child=null;    //child process 33.        try{
            child=Runtime.getRuntime().exec(shellCommand);
        }
        catch(IOException e){
            e.printStackTrace();
        }
        if(child==null){
            return;
        }
        
        final InputStream inputStream=child.getInputStream();
        final BufferedReader brOut=
            new BufferedReader(new InputStreamReader(inputStream));
            
        tOut=new Thread(){    //initialize thread tOut 48.            String line;
            int lineNumber=0;
            public void run(){
                try{
                    while((line=brOut.readLine())!=null){    
                        System.out.println(lineNumber+". "+line);
                        lineNumber++;
                        }
                    }
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        };
            
        final InputStream errorStream=child.getErrorStream();
        final BufferedReader brErr=
            new BufferedReader(new InputStreamReader(errorStream));
        
        tErr=new Thread(){    //initialize thread tErr 68.            String line;
            int lineNumber=0;
            public void run(){
                try{
                    while((line=brErr.readLine())!=null){
                        System.out.println(lineNumber+". "+line);
                        lineNumber++;
                        }
                    }
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        };
        
        // read buffer of parent process' input stream 84.        final BufferedReader reader =
            new BufferedReader(new InputStreamReader(System.in));
        final OutputStream outputStream = child.getOutputStream();
        tIn=new Thread(){
            String line;
            public void run() {
                try {
                    while (true) {
                        outputStream.write( (reader.readLine()+"/n").getBytes());
                        outputStream.flush();
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
           } 
        };
            
    }
    public void startIn(){ //start thread tIn 104.        if(tIn!=null){
            tIn.start();
        }
    }
    public void startErr(){ //start thread tErr 109.        if(tErr!=null){
            tErr.start();
        }
    }
    public void startOut(){ //start thread tOut 114.        if(tOut!=null){
            tOut.start();
        }
    }
    public void interruptIn(){ //interrupt thread tIn 119.        if(tIn!=null){
            tIn.interrupt();
        }
    }
    public void interruptErr(){ //interrupt thread tErr 124.        if(tErr!=null){
            tErr.interrupt();
        }
    }
    public void interruptOut(){ //interrupt thread tOut 129.        if(tOut!=null){
            tOut.interrupt();
        }
    }
    
} 
CODE: mainC.java
----------------------------------------------------------------------------------
public final class mainC {
    public static void main(String[] args) {
        jShell shell=new jShell("ls -l");
        shell.startErr();
        shell.startIn();
        shell.startOut();
    }
}
RESULT:
---------------------------------------------------------------------------------
0. 总用量 44154.1. -rwxrwxrwx    1 root     root          219  5月 12 10:41 ex.pl
2. -rwxrwxrwx    1 root     root          211  5月 12 10:39 ex.pl~
3. -rwxrwxrwx    1 root     root          150  5月 12 10:41 ex.sh
4. -rwxrwxrwx    1 root     root          124  5月 12 10:20 ex.sh~
5. -rwxrwxrwx    1 root     root         1198  5月 12 10:43 jShell$1.class159.6. -rwxrwxrwx    1 root     root         1198  5月 12 10:43 jShell$2.class160.7. -rwxrwxrwx    1 root     root         1222  5月 12 10:43 jShell$3.class161.8. -rwxrwxrwx    1 root     root         2241  5月 12 10:43 jShell.class162.9. -rwxrwxrwx    1 root     root         2720  5月 12 10:43 jShell.java
10. -rwxrwxrwx    1 root     root          544  5月 12 11:43 mainC.class164.11. -rwxrwxrwx    1 root     root          170  5月 12 11:43 mainC.java 
【JAVA中执行bat】
Process  process=Runtime.getRuntime().exec("");中产生停滞(阻塞,blocking),怎么解决?  
---------------------------------------------------------------  
这个是因为Runtime.getRuntime().exec()要自己去处理stdout和stderr的。  
所以如果你想让程序正常运行的话,请务必将上述用别的线程流取走。  
>test.bat  
haha  
exit  99  
>RuntimeTest.java  
public  class  RuntimeTest  {  
           public  static  void  main(String[]  args)  {  
                       try  {  
                                   Process  process=Runtime.getRuntime().exec("test.bat");  
                                   StreamGobbler  errorGobbler  =  new  StreamGobbler(process.getErrorStream(),  "ERROR");                          
                 
                           //  kick  off  stderr   190.                           errorGobbler.start();  
                             
                           StreamGobbler  outGobbler  =  new  StreamGobbler(process.getInputStream(),  "STDOUT");  
                           //  kick  off  stdout   194.                           outGobbler.start();  
                             
                                   process.waitFor();  
                                   System.out.println(process.exitValue());  
                       }  catch(Exception  e)  {}              
           }  
}  
>StreamGobbler.java  
import  java.io.BufferedReader;  
import  java.io.IOException;  
import  java.io.InputStream;  
import  java.io.InputStreamReader;  
import  java.io.OutputStream;  
import  java.io.PrintWriter;  
public  class  StreamGobbler  extends  Thread  {  
       InputStream  is;  
       String  type;  
       OutputStream  os;  
     
       StreamGobbler(InputStream  is,  String  type)  {  
                   this(is,  type,  null);  
       }  

   StreamGobbler(InputStream  is,  String  type,  OutputStream  redirect)  {  
           this.is  =  is;  
           this.type  =  type;  
              this.os  =  redirect;  
      }  
        
      public  void  run()  {  
              try  {  
                      PrintWriter  pw  =  null;  
                      if  (os  !=  null)  
                              pw  =  new  PrintWriter(os);  
                                
                      InputStreamReader  isr  =  new  InputStreamReader(is);  
                      BufferedReader  br  =  new  BufferedReader(isr);  
                      String  line=null;  
                      while  (  (line  =  br.readLine())  !=  null)  {  
                              if  (pw  !=  null)  
                                      pw.println(line);  
                              System.out.println(type  +  ">"  +  line);          
                      }  
                      if  (pw  !=  null)  
                              pw.flush();  
              }  catch  (IOException  ioe)  {  
                      ioe.printStackTrace();      
              }  
      }  


  • 【JAVA 中执行Shell】 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于自己经常利用浏览器插件下载网页图片,由此产生很多的文件夹和文件,这些文件夹的名字多半是网页的title,导致文件夹名很长,文件名多是些很长的数。感觉这样的状况不是很好就自己用java实现了简单的shell来管理这些自动下载的批量文件产生的文件夹和文件! 使用注意事项: 该程序默认的工作路径是c:\盘,建议在windows环境下利用这则小程序,linux下未测试。 这个简单的java shell拥有以下几个命令: cd 进入要工作的目录 ls 列出该工作目录下的文件和文件夹 re 对工作目录下的文件夹名或文件名进行改名操作 man 对具体的命令的详细帮助文档 pwd 显示当前工作的目录 history 显示已经输入的历史命令 help 调取帮助文档命令 exit 退出当前的程序 其re修改文件以及文件夹名的命令是这个程序的价值体现。 re/cd/ls/man这四个命令拥有命令选项,在使用命令选项的规则统一是:xx-uuff其xx代表命令,-代表分隔符是必须的,uu代表命令选项,ff代表具体需求的字符。 pwd/history/help/exit是单个输入的命令,没有命令选项。 利用这样的命令:man-cd就可以查看cd命令使用的具体介绍,其-是必须的,其它带有命令选项的都可以通过这种方式查看命令帮助文档。 例如输入:man-re就会列出以下信息 对当前工作目录下的文件夹名和文件名进行改动的命令: re -j@#将目录下所有的文件夹名字@#间(包括@#)内容去掉 re -q@将目录下所有文件夹名@后(包括@)的内容去掉 re -h@将目录下所有文件夹名@前(包括@)的内容去掉 re -k@将目录下所有文件名的@去掉,当@为.时表示去掉名字的空格(特注) re -m@#$将目录下所有文件名的@#$去掉,这是去掉名字多个字符的命令 re -r@#将目录下所有文件名的@用#替换掉 re -s@#$%将目录下所有文件名的@用#替换,$用%替换,这是替换名字多个字符的命令 re -f+前面七个命令的命令选项的话就是对该工作目录下的所有文件的文件名起相应作用(小心使用)! re -fh.ppp.xxx将目录下所有符合条件的文件的文件名的后缀ppp改成xxx!(较复杂,还未实现) re -fa将当前工作目录下的所有文件名按数字递增方式命!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值