首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Runtime?

如何使用Runtime?
EN

Stack Overflow用户
提问于 2013-04-11 21:21:53
回答 2查看 147关注 0票数 0

我想在java中使用下面给出的commnda。我使用了RunTime。但是对于RunTime,我的结果是所有时间都是空的。

代码语言:javascript
复制
$ cut -d. -f2,3 <<< com.tata.titi.toto
tata.titi

java中使用的方法:

代码语言:javascript
复制
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) {

    }
EN

回答 2

Stack Overflow用户

发布于 2013-04-11 21:26:50

删除process.waitFor();。您正在运行进程,等待其终止,然后在为时已晚时尝试读取其输出。

如果删除此行,则执行该进程并读取其输出。我希望这能对你有所帮助。

顺便说一句,你为什么要这么做?在java中,您可以逐行读取文件并拆分每一行。这更容易,而且是跨平台的。

票数 1
EN

Stack Overflow用户

发布于 2013-04-11 21:55:07

但是在单独的线程中进行错误流处理为什么-请参阅http://www.rgagnon.com/javadetails/java-0014.html http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html

由于一些本机平台仅为标准输入和输出流提供有限的缓冲区大小,未能及时写入输入流或读取子进程的输出流可能会导致子进程阻塞,甚至死锁。

代码语言:javascript
复制
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();
        }

    }

}

这就是你如何使用它

代码语言:javascript
复制
        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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15949918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档