java - 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 循环既不退出也不继续。也不会抛出任何异常。
解决方案
推荐阅读
- sql - 实现 SCD2 的常见使用模式?
- arrays - 使用typedef struct变量作为C中相同typedef struct中其他变量的值
- javascript - Firefox 扩展中的 BlockingResponse
- database - Quarkus:命名数据源、多租户和持久性单元
- swift - RxSwift 在错误事件上调用方法或根本不调用它
- javascript - DatePicker 日历隐藏在表头后面
- sql - 将新记录插入与现有表相关的表中(键和外键)
- python - 根据字典中的键对 python 字典列表进行排序(其中一些列表项缺少这些键)
- html - RMarkdown - 有条件的标题(表格前的文本标题)
- typescript - 从 Nestjs 中的 observable 中的响应返回数据