如何为大型Runtime.Exec操作获取ErrorStream

时间:2012-02-29 17:29:32

标签: java

我正在使用以下代码来获取ErrorStream。

shellinput[0] = "/system/xbin/dd if=/dev/zero of=";
shellinput[1] = newvfspath;
shellinput[2] = "/gtj.img bs=1000000 count=";
shellinput[3] = gtjsize;
System.out
        .println("Error Code (making new "
                + newvfspath
                + "/gtj.img ) :
            + errorstreamReader(shellinput)"
private String errorstreamReader(String[] shellinput) {
    InputStream inputstream = null;
    String esrval = null;
    System.out.println("Entering errorstreamReader");
    //hack to prevent executing null
    for (int i = 0; i <= 3; i++) {
        if (shellinput[i] == null) {
            shellinput[i] = "";
        }
    }
    try {
    System.out.println("Executing " + shellinput[0]
                + shellinput[1] + shellinput[2] + shellinput[3]);
        inputstream = Runtime
                .getRuntime()
                .exec(shellinput[0] + " " + shellinput[1] + " "
                        + shellinput[2]).getErrorStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    InputStreamReader inputstreamreader = null;
    try {
        inputstreamreader = new InputStreamReader(inputstream);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        esrval = new BufferedReader(inputstreamreader).readLine();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return esrval;
}

但问题是/system/xbin/dd if=/dev/zero of=./mnt/sdcard/cimages_2/gtj.img bs=1000000 count=100是一个非常大的操作,需要8秒。所以它没有执行。我该如何解决这个问题?

修改:

问题并不在于它是一项大手术。我弄清楚了。感谢。

1 个答案:

答案 0 :(得分:0)

修正了它:

shellinput[0] = "/system/xbin/dd if=/dev/zero of=";
shellinput[1] = newvfspath;
shellinput[2] = "/gtj.img bs=1000000 count=";
shellinput[3] = gtjsize;
System.out
        .println("Error Code (making new "
                + newvfspath
                + "/gtj.img ) :
            + errorstreamReader(shellinput)"
private String errorstreamReader(String[] shellinput) {
    InputStream inputstream = null;
    String esrval = null;
    System.out.println("Entering errorstreamReader");
    //hack to prevent executing null
    for (int i = 0; i <= 3; i++) {
        if (shellinput[i] == null) {
            shellinput[i] = "";
        }
    }
    try {
    System.out.println("Executing " + shellinput[0]
                + shellinput[1] + shellinput[2] + shellinput[3]);
        inputstream = Runtime
                .getRuntime()
                .exec(shellinput[0]
                + shellinput[1] + shellinput[2] + shellinput[3]).getErrorStream();// this fixed it, typo
    } catch (IOException e) {
        e.printStackTrace();
    }
    InputStreamReader inputstreamreader = null;
    try {
        inputstreamreader = new InputStreamReader(inputstream);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        esrval = new BufferedReader(inputstreamreader).readLine();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return esrval;
}