0

制作一个具有多个函数的类,我遇到了其中一个函数的问题,它将使用 ProcessBuilder 发送命令并将其输出作为字符串,充当 shell 命令。当我使用它时,它会在 InputStreamReader 捕获带有错误文件编号的 IOException ,并返回“错误!!!” ,这是我的代码,如果您需要了解有关此评论的任何信息...我正在使用命令“ls”顺便说一句,“ls /sdcard/”...

     public String builder(String str) {    
    startTimer(); // A method that gets the current time as int
    String[] array = str.split(" " , 2);
    String cmd = array[0] + " " + array[1];
    System.out.println("\nexecuting: " + cmd + "----");
    String AllText = "";
    try {
        String anyString;
        //Process process = new ProcessBuilder("sh","-c","\"",cmd,"\"").start();
        Process process = new ProcessBuilder(array).start();

        BufferedReader OUT = new BufferedReader(new InputStreamReader(process.getInputStream())); // Here is My error
        BufferedReader ERR = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        try {
            for (int x = endTimer()/* a method which subtracts the startTimer() from current time to get the time passed */ ; endTimer()<= 5000 ; x++){
                process.waitFor();
            }process.destroy();

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        while ((anyString = ERR.readLine()) != null) {
            AllText = AllText + "\n" + anyString;
        }
        while ((anyString = OUT.readLine()) != null) {
            AllText = AllText + "\n" + anyString;
            while ((anyString = ERR.readLine()) != null) {
                AllText = AllText + "\n" + anyString;
            }
        }

        return AllText;
    } catch (Exception ex) {
        ex.printStackTrace();
        return "ERROR!!!";

    }

这是按下按钮时的堆栈跟踪:

12-03 18:55:36.475: W/System.err(17973): java.io.IOException: read failed: EBADF (Bad file number)
12-03 18:55:36.507: W/System.err(17973):    at libcore.io.IoBridge.read(IoBridge.java:442)
12-03 18:55:36.507: W/System.err(17973):    at java.io.FileInputStream.read(FileInputStream.java:179)
12-03 18:55:36.507: W/System.err(17973):    at java.io.InputStreamReader.read(InputStreamReader.java:244)
12-03 18:55:36.507: W/System.err(17973):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
12-03 18:55:36.507: W/System.err(17973):    at java.io.BufferedReader.readLine(BufferedReader.java:354)
12-03 18:55:36.507: W/System.err(17973):    at com.example.activity.over.other.Shells.builder(Shells.java:542)
12-03 18:55:36.507: W/System.err(17973):    at com.example.activity.over.other.MainActivity$2.onClick(MainActivity.java:91)
12-03 18:55:36.514: W/System.err(17973):    at android.view.View.performClick(View.java:4202)
12-03 18:55:36.514: W/System.err(17973):    at android.view.View$PerformClick.run(View.java:17340)
12-03 18:55:36.514: W/System.err(17973):    at android.os.Handler.handleCallback(Handler.java:725)
12-03 18:55:36.514: W/System.err(17973):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 18:55:36.514: W/System.err(17973):    at android.os.Looper.loop(Looper.java:137)
12-03 18:55:36.514: W/System.err(17973):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-03 18:55:36.514: W/System.err(17973):    at java.lang.reflect.Method.invokeNative(Native Method)
12-03 18:55:36.522: W/System.err(17973):    at java.lang.reflect.Method.invoke(Method.java:511)
12-03 18:55:36.522: W/System.err(17973):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-03 18:55:36.522: W/System.err(17973):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-03 18:55:36.522: W/System.err(17973):    at dalvik.system.NativeStart.main(Native Method)
12-03 18:55:36.522: W/System.err(17973): Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
12-03 18:55:36.530: W/System.err(17973):    at libcore.io.Posix.readBytes(Native Method)
12-03 18:55:36.530: W/System.err(17973):    at libcore.io.Posix.read(Posix.java:123)
12-03 18:55:36.530: W/System.err(17973):    at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149)
12-03 18:55:36.530: W/System.err(17973):    at libcore.io.IoBridge.read(IoBridge.java:432)
12-03 18:55:36.530: W/System.err(17973):    ... 17 more
4

1 回答 1

-1

Check if you have missed this in the manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-03T16:42:39.990 回答