|
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;
} (编辑:汽车网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|