java启动dos命令收集信息笔记一
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;
public class Person {
@Test //使用testng作为入口
public void doscmd() throws InterruptedException
{
System.out.println(System.getProperty(“os name”)); //判断当前系统
Person p = new Person();
List<String> dL = p.excmd(“adb devices”); //调用函数执行adb devices命令
System.out.println(dL.size());
for (String s:dL) { //通过增强for循环输出内容。 s–定义变量,dl–为adb devices内容
System.out.println(s); //输出显示
}
}
public List<String> excmd(String cmdString) throws InterruptedException {
List<String> dosCMd = new ArrayList<String>(); //定义一个List集合
Process process = null; //定一个Process ID号
try {
process = Runtime.getRuntime().exec(cmdString); //通过内置函数启动exec命令
InputStream in = process.getInputStream(); //通过输出流输出
BufferedReader inR = new java.io.BufferedReader(new InputStreamReader(in));
String line = null;
while((line = inR.readLine()) != null) //通过while遍历命令行信息
{
dosCMd.add(line);
}
System.out.println(“成功取出”);
} catch (IOException e) {
System.out.println(“数据不能获取”);
e.printStackTrace();
}
process.waitFor();
process.destroy();
return dosCMd;
}
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/194466.html