| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2011 人关注过本帖
标题:[求助]java里运行dos命令
只看楼主 加入收藏
yqfang310
Rank: 2
等 级:新手上路
威 望:4
帖 子:458
专家分:0
注 册:2006-10-10
收藏
 问题点数:0 回复次数:9 
[求助]java里运行dos命令

有没有人知道怎么在java里运行系统的cmd程序,也就是模拟dos下命令啊??

搜索更多相关主题的帖子: java dos 命令 运行 
2006-12-05 21:14
weilisky
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-7-20
收藏
得分:0 
Runtime.getRuntime().exec("cmd");
2006-12-05 21:54
weilisky
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-7-20
收藏
得分:0 

Runtime.getRuntime().exec("notepad.exe");
运行记事本哦

2006-12-05 21:57
yqfang310
Rank: 2
等 级:新手上路
威 望:4
帖 子:458
专家分:0
注 册:2006-10-10
收藏
得分:0 
我的意思不是调用系统的cmd,是要自己做一个cmd

JAVA是什么?我也不清楚......
2006-12-05 23:05
常训
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-11-5
收藏
得分:0 
[转载]网上收集的,不知道是不是你要的

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

2006-12-07 17:21
siruyi
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-10-15
收藏
得分:0 
要编病毒啊? 哈哈

2006-12-08 12:01
yqfang310
Rank: 2
等 级:新手上路
威 望:4
帖 子:458
专家分:0
注 册:2006-10-10
收藏
得分:0 

是不是太多了


JAVA是什么?我也不清楚......
2006-12-08 18:19
live
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2006-3-27
收藏
得分:0 
晕了。。
都看不懂了
想哭 才 几个月没有编程就这样了。。。


無舆倫贔<LOvE>
2006-12-11 13:18
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
努力吧

可惜不是你,陪我到最后
2006-12-11 13:19
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 
冰封老大还来的哦!!!  发帖时间是今天中午1点哦!!

开开心心的过&玩每一天!!!!
2006-12-11 18:59
快速回复:[求助]java里运行dos命令
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015502 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved