java - 为什么我的 ProcessBuilder inputStream 产生的数据与来自 linux 命令行的数据不同
问题描述
当我从 Ubuntu 18.4 命令行运行以下命令时:
sudo ./nuvgpio 47 1 > gpioresult-47.txt
生成的文件具有以下数据:
Try 4E
ChipID_Hi=0xD2, ChipID_Lo=0x82.
2, GP48, data=0x00
当我从 Java 运行该过程时,结果流不包含最后一行。
import java.io.*;
/**
* Run a command line program (nuvgpio) to read/write the status of GPIO pins on a Jetway SBC.
*/
public class JetwayIODriver {
private static int LED_PIN_NUMBER = 40;
private static int COVER_PIN_NUMBER = 42;
public static void main(String args[]) throws InterruptedException {
try {
String directoryStr = "/home/tester2/IdeaProjects/automation-framework/device-common-parent/device-io/src/";
File dir = new File(directoryStr);
ProcessBuilder builder = new ProcessBuilder();
builder.directory(dir);
builder.redirectErrorStream();
int mode = 1; // 1=read, 0=write
builder.command("/bin/bash", "-c", "echo 'tester2' | sudo -S ./nuvgpio", Integer.toString(COVER_PIN_NUMBER), Integer.toString(mode));
Process process = builder.start();
InputStreamReader input = new InputStreamReader(process.getInputStream());
BufferedReader reader = new BufferedReader(input);
String line;
while ((line = reader.readLine()) != null) {
System.out.println ("Stdout: " + line);
}
int exitCode = process.waitFor();
input.close();
System.out.println(String.format("GPIO process finished with exit code : %s", exitCode));
} catch (FileNotFoundException fnf) {
System.out.println(fnf.getMessage());
} catch (Throwable t) {
System.out.println(t.getMessage());
}
}
}
Java的输出:
Stdout: Try 4E
Stdout: ChipID_Hi=0xD2, ChipID_Lo=0x82.
GPIO process finished with exit code : 0
需要注意的一件奇怪的事情:当我从命令行运行并输出到控制台时,第三行文本与新提示位于同一行:
nuvgpio 程序似乎没有在末尾放置换行符。这可能是原因吗?如果是这样,我可以在流程输出中注入一个吗?
解决方案
推荐阅读
- scala - 使用 Akka 流分区时,我得到“已经连接”
- c# - 如何修复 WPF 应用程序中的 TargetPlatformMinVersion 问题?
- html - Bootstrap 导航栏下拉菜单
- haskell - 如何使用可扩展效果获得“单子转换器的不灵活语义”?
- javascript - 使用输入框和选择框过滤 html 表
- android - 更改 customView 的层次结构时数据绑定中的 NPE 崩溃
- logstash - 如何在安装期间解决 logstash 错误?
- json - Api 调用返回错误 **keyNotFound....**
- angular - Microsoft graph - 访问令牌验证失败。无效的观众 - 错误
- python-3.x - python:以编程方式在同一文件夹中移动相同的图像