有没有人知道怎么在java里运行系统的cmd程序,也就是模拟dos下命令啊??
import java.io.*;
/**
* <p> Title: 网上收集的资料 </p>
* <p> Description:模拟DOS命令 </p>
* <p>Copyright: </p>
* <p> Company: </p>
* @author
* @version 1.0
*/
class Dos {
private String cmdpath = new String();// CMD命令系统目录路径
private String nowpath = new String();// 当前命令目录 路径
private final String verstr = new String(
"cmd of java linux/windows [版本 1.0.0]\r\n版权:\r\n");
private String cmdstr = new String();// 命令头和参数
private String cmd = new String();// 命令头
private String arg = new String();// 参数
private byte[] readstr = new byte[200];// 命令读入字节数组
private int num;// 命令字节数
Dos(String args[]) {
go();
}
private void go() {
try {
File cmdfile = new File("Dos.class");
String fpath = cmdfile.getAbsolutePath();// CMD类绝对路径
int a;
a = fpath.lastIndexOf("\\");
String fdir = new String(fpath.getBytes(), 0, a);// cmd类目录
cmdpath = new String(fdir);
nowpath = new String(cmdpath);
} catch (Exception e) {
System.out.println("cmd命令系统初始化失败");
System.exit(1);
}
System.out.println(verstr);
while (true) {
cmd = "";
arg = "";
System.out.print(nowpath + ">");
try {
num = System.in.read(readstr);
} catch (Exception e) {
System.out.println("输入异常");
}
cmdstr = new String(readstr, 0, num - 2);
getcmd();
getarg();
if (cmd.compareTo("exit") == 0)
System.exit(0);
if (cmd.compareTo("cd") == 0) {
cd();
continue;
}
if (cmd.compareTo("dir") == 0) {
dir();
continue;
}
if (cmd.compareTo("cls") == 0) {
cls();
continue;
}
if (cmd.compareTo("ver") == 0) {
ver();
continue;
}
if (cmd.compareTo("type") == 0) {
type();
continue;
}
if (cmd.compareTo("md") == 0) {
md();
continue;
}
if (cmd.compareTo("del") == 0) {
del();
continue;
}
}// while
}// go
// 读取命令头
private void getcmd() {
char cmdchar[];
cmdchar = cmdstr.toCharArray();
if (cmdchar[0] != ' ')// 第一个是不是空格
{
int n;
n = cmdstr.indexOf(" ");
if (n != -1)// 有空格
cmd = new String(cmdstr.getBytes(), 0, n);
else
cmd = new String(cmdstr);
}// 第一个不是空格
else {
int i = 0;
for (i = 0; i < cmdchar.length; i++) {
if (cmdchar[i] != ' ')// 第1个不为空格的 取得i的值
{
String s = cmdstr.substring(i);
int n = s.indexOf(" ");
if (n != -1) {
cmd = new String(s.getBytes(), 0, s.indexOf(" "));
break;
} else {
cmd = new String(s);
break;
}
}// if
}// for
}// 第一个是空格
}// getcmd() //取得命令参数
private void getarg() {
int a = cmdstr.indexOf(cmd);
a += cmd.length();
String args = cmdstr.substring(a);
char argchar[] = args.toCharArray();
for (a = 0; a < argchar.length; a++) {
if (argchar[a] != ' ') {
arg = args.substring(a);
break;
}// if
}// for
}// getarg()
// DIR命令
private void dir() {
int max = 0;
if (arg.compareTo("") == 0) {
File f;
try {
f = new File(nowpath);
} catch (Exception e) {
System.out.println("文件系统错误");
return;
}
String fss[] = f.list();
int i;
// 得到文件名的最长值
for (i = 0; i < fss.length; i++) {
if (fss[i].length() > max) {
max = fss[i].length();
}
}
max += 10;
System.out.println(f + "目录清单:");
for (i = 0; i < fss.length; i++) {
System.out.print(fss[i]);
}
int k_max = 0;
for (k_max = 0; k_max < fss.length; k_max++) {
System.out.print(" ");
File a = new File(nowpath + "\\" + fss[i]);
if (a.isDirectory() == true) {
System.out.print("" + "\r\n");
} else {
System.out.print(a.length() + " b\r\n");
}
}
return;
}// 当dir命令参数为空处理
try {
int aa;
aa = arg.indexOf(":");
if (aa != -1) {
File f = new File(arg);
boolean ok;
ok = f.exists() && f.isDirectory();
if (ok == true)// 目录存在
{
String fss[] = f.list();
int i;
// 得到文件名的最长值
System.out.println(f + "目录清单:");
for (i = 0; i < fss.length; i++) {
if (fss[i].length() > max) {
max = fss[i].length();
}
}
max += 10;
for (i = 0; i < fss.length; i++) {
System.out.print(fss[i]);
int k_max = 0;
for (k_max = 0; k_max < fss.length; k_max++) {
System.out.print(" ");
File a = new File(arg + "\\" + fss[i]);
if (a.isDirectory() == true) {
System.out.print("" + "\r\n");
} else {
System.out.print(a.length() + " b\r\n");
}
}
return;
}
}
} else// 目录不存在
{
System.out.println("目录" + arg + "不存在");
return;
}
} catch (Exception e) {
return;
}// 当参数含有盘符时
// 一般参数时
try {
File f = new File(nowpath + "\\" + arg);
boolean ok;
ok = f.exists() && f.isDirectory();
if (ok == true)// 目录存在
{
String fss[] = f.list();
int i;
// 得到文件名的最长值
System.out.println(f + "目录清单:");
for (i = 0; i < fss.length; i++) {
if (fss[i].length() > max) {
max = fss[i].length();
}
}
max += 10;
for (i = 0; i < fss.length; i++) {
System.out.print(fss[i]);
int k_max = 0;
for (k_max = 0; k_max < fss.length; k_max++)
System.out.print(" ");
File a = new File(nowpath + "\\" + arg + "\\" + fss[i]);
if (a.isDirectory() == true) {
System.out.print("" + "\r\n");
} else {
System.out.print(a.length() + " b\r\n");
}
}
return;
} else// 目录不存在
{
System.out.println("目录" + arg + "不存在");
return;
}
} catch (Exception e) {
return;
}
}// dir()
// CD命令
private void cd() {
int a = arg.indexOf(":");
if (a != -1) {// 如果CD参数有盘符
try {
File nf = new File(arg);
boolean ok;
ok = nf.exists() && nf.isDirectory();
if (ok == true) {
String fpath = nf.getAbsolutePath();
fpath += "\\";
a = fpath.lastIndexOf("\\");
String fdir = new String(fpath.getBytes(), 0, a);
nowpath = new String(fdir);
}// if
else
System.out.println("目录" + arg + "不存在");
}// try
catch (Exception e) {
System.out.println("3:" + e);
}// catch
return;
}// if CD参数中有盘符
if (arg.compareTo(".") == 0)
return;
if (arg.compareTo("\\") == 0) {
int arg_m = nowpath.indexOf(":");
String np = new String(nowpath.getBytes(), 0, arg_m + 1);
nowpath = np + "\\";
return;
}
if (arg.indexOf("..") != -1) {
a = nowpath.lastIndexOf("\\");
if (a != -1) {
int arg_d = arg.indexOf("..");
String aa = arg.substring(arg_d + 2);
String s = new String(nowpath.getBytes(), 0, a);
s += aa;
nowpath = new String(s);
}
return;
}// 参数为 ..
// 一般参数处理
String tempath = new String(nowpath + "\\" + arg);
try {
File tempf = new File(tempath);
boolean ok;
ok = tempf.exists() && tempf.isDirectory();
if (ok == true) {
nowpath = new String(tempf.getAbsolutePath());
return;
}// 目录真的存在
else
System.out.println("目录" + arg + "不存在");
} catch (Exception e) {
System.out.println("目录" + arg + "不存在");
}
return;
}// cd()
// ver命令
private void ver() {
System.out.println(verstr);
}// ver()
// 主程序
public static void main(String args[]) {
new Dos(args);
}
// type命令
private void type() {
File l = new File(arg);
boolean ok;
ok = l.exists();
if (ok == false) {
System.out.println("文件" + arg + "不存在");
return;
}
try {
FileInputStream f = new FileInputStream(arg);
int num = f.available();
byte b[] = new byte[num];
f.read(b);
System.out.println(new String(b));
} catch (Exception e) {
System.out.println("文件系统错误");
return;
}
}// main()
// del命令
private void del() {
if (arg.length() == 0)
return;
File f;
int a;
a = arg.indexOf(":");
if (a != -1)
f = new File(arg);
else
f = new File(nowpath + "\\" + arg);
boolean ok;
ok = f.exists();// 是否存在
if (ok == false) {
System.out.println(arg + "不存在");
return;
}
ok = f.isDirectory();// 是不是目录
if (ok == false)// 为文件时
{
ok = f.delete();
if (ok == false)
System.out.println("无法删除" + arg);
return;
}
String s[] = f.list();
if (s.length != 0) {
System.out.println("目录不为空\r\n下面是目录信息:");
// dir();
return;
}
ok = f.delete();
if (ok == false)
System.out.println("无法删除" + arg);
return;
}// dir()
// md命令
private void md() {
if (arg.length() == 0)
return;
File f;
int a;
a = arg.indexOf(":");
if (a != -1)
f = new File(arg);
else
f = new File(nowpath + "\\" + arg);
boolean ok;
ok = f.exists();
if (ok == true) {
System.out.println(arg + "以经存在");
return;
}
ok = f.mkdirs();
if (ok == false)
System.out.println("系统错误,无法新建目录");
}// md()
//cls命令
private void cls() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("cmd.exe /c dir \\windows");
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}//class cmd