2013-04-11 73 views
0

我想在java中使用下面給出的commnda。我使用了RunTime。但有了RunTime,我的所有時間都是空的。如何使用Runtime?

$ cut -d. -f2,3 <<< com.tata.titi.toto 
tata.titi 

在Java中使用的方法,包括:

public void tataName() { 

    try { 
     Process process = Runtime.getRuntime().exec(
       new String[] { "/bin/sh", "cut -d. -f2,3 <<< com.tata.titi.toto " }); 
     process.waitFor(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       process.getInputStream())); 
     File f = new File(path+ "/taname.txt"); 
     PrintWriter writer = new PrintWriter(f, "UTF-8"); 
     String line = reader.readLine(); 
     while ((line != null)) { 
      System.out.println(line); 
      writer.println(line); 
      line = reader.readLine(); 



     } 

     writer.close(); 


    } catch (IOException | InterruptedException e1) { 

    } 
+0

HTTP:// WWW。 rgagnon.com/javadetails/java-0014.html但我會使用版本與輸入和錯誤STRAM只是等待單獨的線程上的錯誤,所以一個進程可以發出很少的輸出,然後錯誤,然後更多的輸出.... – tgkprog 2013-04-11 13:50:40

回答

1

process.waitFor();刪除。您正在運行進程,等待其終止,然後嘗試在其太晚時讀取其輸出。

如果您刪除此行,則執行該過程並讀取其輸出。我希望這能幫到您。

順便說一句,你爲什麼這樣做?您可以逐行讀取文件,並在java中分割每一行。這是很多更容易和跨平臺。

+0

有些時候,我將有路徑/ com.tata.titi.toto。我不知道是否更容易使用拆分。事實上,我有一個與許多應用程序的文件夾,命名爲他們的包如com.tata.titi.toto.apk,應用程序的名稱是tata.titi。我想有一個地圖<包,應用程序名稱> – diez 2013-04-11 13:38:48

+0

它不是等待我用process.wait的一些綜合的問題,因爲它是好的,當我更改命令時,我有我想要的。 – diez 2013-04-11 13:41:48

0

http://www.rgagnon.com/javadetails/java-0014.html但在一個單獨的線程做錯誤流處理爲什麼 - 見的javadoc http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html

因爲有些本機平臺僅針對 標準輸入和輸出流提供有限的緩衝區大小,未能及時寫輸入 流或讀取子流程的輸出流可能會導致子進程阻塞,甚至死鎖。

package utl; 

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileReader;  

public class ReadStreamAsync extends Thread { 

    private BufferedReader is = null; 
    private int toOut = 0;//0 none, 1 out, 2 err 
    private boolean toSB = false; 
    private StringBuffer sb = null; 
    public ReadStreamAsync(BufferedReader is, int toOut, boolean toSB){ 
     if(is == null)throw new NullPointerException("stream is null"); 
     this.is = is; 
     this.toSB = toSB; 
     this.toOut = toOut; 
     if(toSB)sb = new StringBuffer(); 
     start(); 
    } 

    public void run(){ 
     try{ 
      int i; 
      while((i = is.read()) > -1){ 
       if(toOut == 1){ 
        System.out.print((char)i); 
       }else if(toOut ==2){ 
        System.err.print((char)i); 
       } 
       if(toSB)sb.append((char)i); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     }finally{ 
        try{ 
          is.close(); 
        }catch(Exception e){ 
          e.printStackTrace(); 
        } 
       } 
    } 

    public String getRead(){ 
     return sb.toString(); 
    } 

    /** 
     * test code 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try{ 
      BufferedReader fis = new BufferedReader(new FileReader("c:/tmp/sample1.txt")); 
      ReadStreamAsync t = new ReadStreamAsync(fis, 1, false); 
      t.join(1000); 
      System.out.println("\nAll :"); 
      fis = new BufferedReader(new FileReader("c:/tmp/sample1.txt")); 
      t = new ReadStreamAsync(fis, 0, true); 
      t.join(1000); 
      System.out.println(t.getRead()); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 

} 

這就是你如何使用它

 Process proc = procBldr.start(); 
     //props is a java.util.properties that was previosly loaded from some stream 
     System.out.println(" Cmd start f :" + cmdStartFolder); 
     System.out.println(" Cmd :" + lstCmds); 
     BufferedReader isO = new BufferedReader (new InputStreamReader(proc.getInputStream())); 
     BufferedReader isE = new BufferedReader (new InputStreamReader(proc.getErrorStream())); 
     asynO = new com.enstage.commonutil.file.ReadStreamAsync(isO, 1, true); 
     asynE = new com.enstage.commonutil.file.ReadStreamAsync(isE, 1, true); 
     if("1".equals(props.getProperty("waitFor")){ 
      proc.waitFor();//maybe parameterize this not required everywhere only good for short processes 
     } 
     String sleepAfterWait = props.getProperty("sleepAfterWait"); 
     try { 
      Thread.sleep(500);//some sleep after telling windows to do things with files is good 
      if(sleepAfterWait != null){ 
       int i = Integer.parseInt(sleepAfterWait); 
       Thread.sleep(i); 
      } 
     } catch (Exception e) { 
      System.out.println("sleep err :" + e); 
      e.printStackTrace(); 
     } 
     asynE.join(); 
     asynO.join(); 
     String checkString = props.getProperty("checkString"); 
     System.out.println("\n done "); 
     //asynE.getRead();//if you want error out as a string 
     if(checkString != null){ 
      System.out.println("checkString :" + checkString); 
      String out = asynO.getRead(); 
      if(out.indexOf(checkString) > -1){ 
       System.out.println(" Check string found!"); 
      }else{ 
       System.err.println(" *** Check string not found ***!"); 
      } 
     } 

已經用它成功地調用XCOPY在蝙蝠和其他進程短期和長期

相關問題