简体   繁体   English

"如何读取控制台输出的最后一行?"

[英]How do I read the last line in the console output?

So im trying to make a Java program that uses adb and fastboot to root my Nexus 6P.所以我试图制作一个使用 adb 和 fastboot 来根植我的 Nexus 6P 的 Java 程序。 I have a class that can run the commands, but I need to capture the device ID from the output.我有一个可以运行命令的类,但我需要从输出中捕获设备 ID。 when I run adb<\/code> or fastboot<\/code> the output either looks like one of the three options below当我运行adb<\/code>或fastboot<\/code>时,输出看起来像以下三个选项之一

List of devices attached
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
5VT7N16324000434          device

I'm not sure but the only solution that come's up to my mind, is to route the PrintStream<\/code> ( System.out<\/code> ) and assign every line that gets printed to a String called lastLine<\/code> or something.我不确定,但我想到的唯一解决方案是路由PrintStream<\/code> ( System.out<\/code> )并将打印的每一行分配给名为lastLine<\/code>的字符串或其他东西。 You can then get the device ID using a regex expression that removes the spaces\/tabs after the actual device ID然后,您可以使用删除实际设备 ID 后的空格\/制表符的正则表达式获取设备 ID
like this -> ( DEVICEID device<\/code> ) to ( DEVICEID<\/code> ) or just replace every space using String.replaceAll(" ", "");<\/code>像这样 -> ( DEVICEID device<\/code> ) 到 ( DEVICEID<\/code> ) 或者只是使用String.replaceAll(" ", "");<\/code>替换每个空格and the "device" word using String.replace("device", "");<\/code>和“设备”字使​​用String.replace("device", "");<\/code> . . I hope I could help you, as I'm fairly new to this community.我希望我能帮助你,因为我对这个社区还很陌生。

Edit:编辑:
Here's the code that assigns the last line to a String:这是将最后一行分配给字符串的代码:


private String lastLine;

public void construct() {
    System.setOut(new PrintStream(System.out) {
        public void println(String s) {
            lastLine = s;
            super.println(s);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM