首页 > 解决方案 > InputStream:无法读取字符串

问题描述

我想通过输入流读取get-wmiobject -class win32_printer | Select-Object Name, WorkOffline | where {$_.Name -eq 'printerName'}ProcessBuilder 从 Powershell 发出的命令结果,以了解打印机当前是否连接到计算机。如果我的功能在这里

public static String getStringFromInputStream(InputStream is) {
    System.out.println(66);
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    System.out.println(68);
    byte[] buffer = new byte[1024];
    int length;
    try {
        System.out.println(70);
        while ((length = is.read(buffer)) != -1) {
            System.out.println(74);
            result.write(buffer, 0, length);
            System.out.println(76);
        }
    } catch (IOException e1) {
        System.out.println("79");
        e1.printStackTrace();
    }

    String finalResult = "";
    System.out.println(82);
    finalResult = result.toString("CP866");
    System.out.println(84);
    return finalResult;
}

如您所见,为了调试,我想打印出代码行。这是我在控制台中的输出:

66
68
70
74
76
74
76
74
76
74
76
74
76

如您所见,while 循环既不退出也不继续。也不会抛出任何异常。

标签: javainputstreamprocessbuilder

解决方案


推荐阅读