JAVA写的CMD

来源:互联网 发布:网络销售好无聊 编辑:程序博客网 时间:2024/04/29 00:07
/*
清频那个cls命令还不知道怎么写呢?
*/
import java.io.*;
class cmd
{
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帮助:http://nicend.bokee.com/r/n版权:nicend/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;//命令字节数
cmd(String args[])
{go();}
private void go()
{
try{
File cmdfile=new File("cmd.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<cmdstr.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<args.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(max<fss[i].length())
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<max-fss[i].length();k_max++)
System.out.print(" ");
File a=new File(nowpath+"//"+fss[i]);
if(a.isDirectory()==true)
System.out.print("<DIR>"+"/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(max<fss[i].length())
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<max-fss[i].length();k_max++)
System.out.print(" ");
File a=new File(arg+"//"+fss[i]);
if(a.isDirectory()==true)
System.out.print("<DIR>"+"/r/n");
else
System.out.print(a.length()+" b/r/n");
}
return;
}
else//目录不存在
{System.out.println("目录"+arg+"不存在");return;}
}
}//try
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(max<fss[i].length())
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<max-fss[i].length();k_max++)
System.out.print(" ");
File a=new File(nowpath+"//"+arg+"//"+fss[i]);
if(a.isDirectory()==true)
System.out.print("<DIR>"+"/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[])
{
cmd a=new cmd(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,isfile;
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()
{}
}//class cmd

- 作者: 成长石子 2006年02月15日, 星期三 15:30  回复(0) |  引用(0) 加入博采

文件分割程序(JAVA)
import java.io.*;
class mycut
{
public static void main(String args[]) throws Exception
{
int num;
String fn;//文件名
String fss;//分割份数]
int fs;//分割份数
FileInputStream fin;
FileOutputStream fbat;
FileOutputStream fout;
String name;
File f;
byte[] fb=new byte[1024];
int fsum=0;//用于核对文件大小
System.out.println("欢迎使用nicend文件分割器——http://nicend.bokee.com");
System.out.print("请输入要分割的文件名:");
num=System.in.read(fb);
fn=new String(fb,0,num-2);
f=new File(fn);
System.out.println("文件大小:"+f.length()+" b");
System.out.print("请输入分割文件份数:");
num=System.in.read(fb);
fss=new String(fb,0,num-2);
fs=Integer.parseInt(fss);
if(fs<2)
{System.out.print("份数最少为2");System.exit(0);}
System.out.print("开始分割.../n");
name=new String(fn.getBytes(),0,fn.lastIndexOf("."));
File fdir=new File(".//"+name+"分割文件");
fdir.mkdirs();
fin=new FileInputStream(fn);//原文件
fbat=new FileOutputStream(fdir.toString()+"//"+name+"合并"+".bat");
fbat.write(new String("copy /b "+name+"0.nicend"+"+"+name+"1.nicend "+fn).getBytes());
byte[] ch=new byte[new Long(f.length()/fs+1).intValue()];
int i=0;
int sum=0;
num=fin.read(ch);
while(num!=-1)
{
fout=new FileOutputStream(fdir.toString()+"//"+name+i+".nicend");
if(i>1)
fbat.write(new String("/r/ncopy /b "+fn+"+"+name+i+".nicend").getBytes());//这里要用/b是以二进制运行
fout.write(ch,0,num);
fsum+=num;
System.out.println(num);
fout.close();
i++;
num=fin.read(ch);
}//while
System.out.println("        :="+fsum);
fin.close();
fbat.close();
if(f.length()==fsum)//判断分割是否成功,即文件大小是否一样
System.out.print("分割完成!");
else
System.out.print("分割出错,文件大小不一至!");
}//main
}//class
原创粉丝点击