gpt4 book ai didi

java - 提取字符串的设备名称

转载 作者:行者123 更新时间:2023-11-30 07:01:13 25 4
gpt4 key购买 nike

我正在尝试使用 “adb devices” 命令提取 android 设备名称..通过使用这种方法我成功地得到了:

public void newExec() throws IOException, InterruptedException, BadLocationException{
String adbPath = "/Volumes/development/android-sdk-macosx/tools/adb";
String cmd = adbPath+" "+"devices";

Process p;
p = Runtime.getRuntime().exec(cmd);
p.waitFor();

String line;

BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = err.readLine()) != null) {
System.out.println(line);
}
err.close();

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line=input.readLine()) != null) {
//printing in the console
System.out.println(line);
}
input.close();
}

输出是:

List of devices attached
192.168.56.101:5555 device

我试图从此输出中仅获取设备名称:

192.168.56.101:5555

我以多种方式使用拆分,例如:

String devices = "List of devices attached";
System.out.println(line.split(devices);

但这根本不起作用!

我不想要静态方式,而是动态方式。我的意思是,如果设备名称更改或列出的设备不止一个,我想要一种只提供设备名称的方法。有办法吗?

抱歉,如果问题不是那么清楚,我对 Java 有点陌生:)

最佳答案

你可以试试下面的代码:

adb devices 的下一行输出由制表符分隔,因此我们必须使用“\t”作为参数。

List<String> deviceList = new ArrayList<String>();

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if (line.endsWith("device")) {
deviceList.add(line.split("\\t")[0]);
}

}

for (String device : deviceList) {
System.out.println(device);
}

关于java - 提取字符串的设备名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com