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

时间:2017-02-13 02:09:46

标签: java console output console-application

所以我试图创建一个使用adb和fastboot来生成我的Nexus 6P的Java程序。我有一个可以运行命令的类,但我需要从输出中捕获设备ID。当我运行<svg style="position: absolute; z-index: 100;" width="100%" height="100%"> <defs> <path id="doorFillInvPath" d="M-1000,-1000 v2000 h2000 v-2000 h-2000 M0,0 h-50 a50,50 0 0 1 50,-50 Z"/> <path id="doorFillPath" d="M0,0 h-50 a50,50 0 0 1 50,-50 Z"/> <clipPath id="doorFillInv1" transform="translate(210,100) rotate(45)"> <use href="#doorFillInvPath"/> </clipPath> <clipPath id="doorFillInv2" transform="translate(210,200) rotate(60)"> <use href="#doorFillInvPath"/> </clipPath> <line id="door" x1="0" y1="0" x2="0" y2="-50"/> </defs> <g transform="scale(0.6) translate(10,40)"> <g class="floor"> <use href="#doorFillPath" transform="translate(210,200) rotate(60)"/> <path id="room1" d="M10,10 h200 v250 h-200 z" stroke="none" clip-path="url(#doorFillInv1)"/> </g> <g class="floor"> <use href="#doorFillPath" transform="translate(210,100) rotate(45)"/> <path id="room2" d="M210,10 h150 v100 h50 v150 h-200 z" stroke="none" clip-path="url(#doorFillInv2)"/> </g> <use href="#door" transform="translate(210,100) rotate(-45)"/> <use href="#door" transform="translate(210,200) rotate(60)"/> <path id="walls" d="M210,10 h150 v100 h50 v150 h-200 v-60 m0,-50 v-50 m0,-50 v-40 h-200 v250 h200"/> </g> </svg> <p>Mouse over (i.e. hover over) each room to see the floor fills.</p> <p style="color: #ccc;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nunc neque, molestie quis erat in, dignissim pretium nulla. Maecenas eget pretium purus. Aliquam interdum at dolor id sagittis. Phasellus risus dui, consectetur ac tortor ac, dignissim consequat felis. Maecenas lacinia ac arcu in porttitor. Duis aliquam maximus quam, vel venenatis dui. Quisque placerat ligula sit amet finibus vestibulum. Suspendisse tristique pharetra eleifend. Fusce nisl lectus, gravida vitae elit sed, vehicula condimentum orci. Sed dapibus pharetra odio ultricies egestas. Quisque pretium vehicula ante in commodo. Nulla eget turpis ac sem tristique posuere ac non diam. Aliquam at pulvinar enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nunc neque, molestie quis erat in, dignissim pretium nulla. Maecenas eget pretium purus. Aliquam interdum at dolor id sagittis. Phasellus risus dui, consectetur ac tortor ac, dignissim consequat felis. Maecenas lacinia ac arcu in porttitor. Duis aliquam maximus quam, vel venenatis dui. Quisque placerat ligula sit amet finibus vestibulum. Suspendisse tristique pharetra eleifend. Fusce nisl lectus, gravida vitae elit sed, vehicula condimentum orci. Sed dapibus pharetra odio ultricies egestas. Quisque pretium vehicula ante in commodo. Nulla eget turpis ac sem tristique posuere ac non diam. Aliquam at pulvinar enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nunc neque, molestie quis erat in, dignissim pretium nulla. Maecenas eget pretium purus. Aliquam interdum at dolor id sagittis. Phasellus risus dui, consectetur ac tortor ac, dignissim consequat felis.</p>adb时,输出看起来像下面的三个选项之一

fastboot

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

List of devices attached
5VT7N16324000434          device

现在我需要捕获5VT7N16324000434 device 并将其保存为字符串。虽然它并不总是一样的。我搜索了几个小时,没有任何帮助,也不知道从哪里开始。

我用这个类运行adb

5VT7N16324000434

然后我需要阅读import java.io.BufferedReader; import java.io.InputStreamReader; public class ExecuteShellCommand { public static void main(String inputCommand) { ExecuteShellCommand obj = new ExecuteShellCommand(); String command = inputCommand; String output = obj.executeCommand(command); System.out.println(output); } private String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } return output.toString(); } } 的输出或者可能在那个地方的任何字符串。

0 个答案:

没有答案