java 运行CMD命令,并读取命令行输出的数据
public class TestCMD { public static void test01() throws IOException { Runtime runtime = Runtime.getRuntime(); // 运行命令,获取当前任务列表 Process process = runtime.exec("tasklist"); /* // 将控制台输出的数据通过InputStreamReader类传给BufferedReader InputStreamReader类是从字节流到字符流的桥接器 BufferedReader类从字符输入流中读取文本并缓冲字符,以便有效地读取字符,数组和行 */ BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); String s = ""; //读取每一行 while((s=br.readLine())!=null){ System.out.println(s); } } public static void main(String[] args) throws IOException { test01(); } }