提取字符串的设备名称
Extracting device name of a string
我正在尝试使用 "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 有点陌生:)
我不太熟悉 Android 编程,但对我来说这听起来像是一个简单的字符串解析问题,而不是特定于 android。无论如何,我的 2 美分在这里。您可以尝试解析仅以
结尾的行
String line;
List<String> devices = new ArrayList<String>();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line=input.readLine())!=null){
//printing in the console
System.out.println(line);
if (!line.endsWith("device")) {
//skip if it does not ends with suffix 'device'
continue;
}
else {
//parse it now
String[] str = line.split(" ");
devices.add(str[0]);
}
}
看来您使用的 String
split()
方法有误。
String devices = "List of devices attached";
System.out.println(line.split(devices);
使用示例:
String[] ss = "This is a test".split("a");
for (String s: ss )
System.out.println(s);
输出
This is
test
split(String regex)
的参数必须是正则表达式(regex)。
此外,您可以使用 StringTokenizer
class 或 Scanner
class。这些 classes 有更多的标记化选项。
您可以尝试以下代码:
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);
}
使用下面的代码(注意:只有每次输出的字符串都一样才有效)
String devices = "List of devices attached 192.168.56.101:5555 device";
String[] str = devices.split(' '); //spliting the string from space
System.out.println(str[4]);
输出:
192.168.56.101:5555
希望对您有所帮助。
我正在尝试使用 "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 有点陌生:)
我不太熟悉 Android 编程,但对我来说这听起来像是一个简单的字符串解析问题,而不是特定于 android。无论如何,我的 2 美分在这里。您可以尝试解析仅以
结尾的行 String line;
List<String> devices = new ArrayList<String>();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line=input.readLine())!=null){
//printing in the console
System.out.println(line);
if (!line.endsWith("device")) {
//skip if it does not ends with suffix 'device'
continue;
}
else {
//parse it now
String[] str = line.split(" ");
devices.add(str[0]);
}
}
看来您使用的 String
split()
方法有误。
String devices = "List of devices attached";
System.out.println(line.split(devices);
使用示例:
String[] ss = "This is a test".split("a");
for (String s: ss )
System.out.println(s);
输出
This is
test
split(String regex)
的参数必须是正则表达式(regex)。
此外,您可以使用 StringTokenizer
class 或 Scanner
class。这些 classes 有更多的标记化选项。
您可以尝试以下代码:
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);
}
使用下面的代码(注意:只有每次输出的字符串都一样才有效)
String devices = "List of devices attached 192.168.56.101:5555 device";
String[] str = devices.split(' '); //spliting the string from space
System.out.println(str[4]);
输出:
192.168.56.101:5555
希望对您有所帮助。