获取在控制台输入命令后的结果

背景:在控制台输入命令如查询手机设备的命令,需要获取到控制台的内容

实现代码,主要是当文件流的方式实现

public static void main(String[] args) {
        Process process = null;
        List<String> processList = new ArrayList<String>();
        System.out.println("执行命令开始:");
        try {
            process = Runtime.getRuntime().exec("adb devices");//查询adb链接的数据
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));//文件流方式
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        for (String string : processList) {
            System.out.println("结果:"+string);
        }
    }

打印的结果

原文地址:https://www.cnblogs.com/chongyou/p/7508782.html