java-执行dos命令

时间:2021-05-15 08:11:20
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random; public static void main(String[] args) throws InterruptedException
{
try{
File dir = new File("D:\\");
String dos = "c:\\windows\\system32\\cmd.exe /c dir";
//"c:\\windows\\system32\\cmd.exe --cmd名利地址
// /c
//dir--cmd 命令行中执行的命令,列出文件";
// java执行命令行程序 dos命令行,dir:命令行执行的目录
Process p = Runtime.getRuntime().exec(dos,null,dir); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer s = new StringBuffer();
String line = null;
while((line= br.readLine()) != null)
{
// 将执行结果打印到控制台
System.out.println(line); } }catch(IOException e)
{
e.printStackTrace();
} }