使用cmd

Posted dongbo-2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用cmd相关的知识,希望对你有一定的参考价值。



public class DeviceInfo {
public static String execute(String cmd) {
System.out.println(cmd);

StringBuilder sb = new StringBuilder();
StringBuilder error = new StringBuilder();
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
sb.append(" ");
}
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = errorReader.readLine()) != null) {
error.append(line);
error.append(" ");
}
} catch (Exception e) {
e.printStackTrace();
}
// String err = error.toString();
System.out.println(error.toString());

return sb.toString();
}



public static Map<String, String> getDevices() {
String t = execute("adb devices");
String[] ds = t.split(" ");
Map<String, String> r = new HashMap<String,String>();
for (int i = 1; i < ds.length; i++) {
ds[i] = ds[i].split(" | ")[0];
r.put(execute(ADB.getPhoneInfoBase(ds[i])).replace(" ", ""), ds[i]);
}
return r;
}


public static String execute(String cmd, ResultCallback resultCallback) {

// System.out.println(cmd);

int count = 0;
StringBuffer sb = new StringBuffer();
StringBuffer error = new StringBuffer();
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;

while ((line = bufferedReader.readLine()) != null /*&& count < 5*/) {
//long start = System.currentTimeMillis();
//count++;

sb.append(line);
sb.append(" ");
resultCallback.receiveLine(line + " " );
// long end= System.currentTimeMillis();
// if (line.contains("Escape")) {
// break;
// }
// if(end-start>5*1000)
// {
// break;
// }

}

} catch (Exception e) {
e.printStackTrace();
}
// String err = error.toString();
System.out.println(error.toString());

return sb.toString();
}
}

以上是关于使用cmd的主要内容,如果未能解决你的问题,请参考以下文章

在 CMD 中使用 CreateProcessWithTokenW 启动 CMD

如何使用Java执行cmd命令

CMD命令使用方法

计算机cmd命令如何使用?

如何使用CMD命令行

计算机cmd命令如何使用