0

以下のコマンドをJavaで使用したいと思います。ランタイムを使用しました。しかし、RunTime を使用すると、結果として常に null になります。

$ 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) {

    }
4

2 に答える 2

1

削除しprocess.waitFor();ます。プロセスを実行し、その終了を待ってから、手遅れになったときにその出力を読み取ろうとします。

この行を削除すると、プロセスが実行され、その出力が読み取られます。これがお役に立てば幸いです。

ところで、なぜあなたはこれをしているのですか?ファイルを 1 行ずつ読み取り、各行を Java で分割できます。これははるかに簡単で、クロスプラットフォームです。

于 2013-04-11T13:26:50.020 に答える
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 ***!" );
            }
        }

それを使用して、.batおよび他のプロセスでxcopyを短くおよび長く呼び出すことに成功しました

于 2013-04-11T13:55:06.667 に答える