吐槽= =.2点睡,一直被热到3点,起床编程算了.
实现部分命令,或许存在一些bug
命令有
mov,cd,md,copy,cls,exit,dir,type,run运行程序
import java.io.*;
import java.util.*;
public class shell1 {
StringBuffer sourcePath = new StringBuffer("C:\\Users\\Administrator");
/*
* 程序入口
* @author anima_libera
*/
public static void main(String args[]) throws IOException{
shell1 shell = new shell1();
shell.choicecom();
}
/*
* 选择入口
*/
public void choicecom() throws IOException{
String com,strcom[];
Scanner sc = new Scanner(System.in);
System.out.println("输入命令");
while(true){
System.out.print(sourcePath + ">");
com = sc.nextLine();
com.trim();
if(com.toLowerCase().endsWith("exit")||com.toLowerCase().endsWith("quit"))
break;
strcom = com.split(" ");
choice(strcom);
}
}
/*
* 命令入口,预留可扩展
*/
public void choice(String[] com)
throws IOException{
if(com[0].toLowerCase().startsWith("dir")){
if(com[0].toLowerCase().endsWith("dir/?")){
System.out.println("显示指定路径下的所有文件,文件夹");
}else{
if(com.length > 1)
comdir(com[1]);
else
comdir(" ");
}
}else
if(com[0].toLowerCase().startsWith("md")){
if(com[0].toLowerCase().endsWith("md/?")){
System.out.println("在指定路径下建立一个子目录");
}else{
if(com.length > 1)
commd(change(com[1]));
else
System.out.println("不能建立空文件夹");
}
}else
if(com[0].toLowerCase().startsWith("cd")){
if(com[0].toLowerCase().endsWith("cd/?")){
System.out.println("可使当前的目录改为指定的一个目录路径下");
}else{
if(com.length > 1){
comcd(com[1]);
}
}
}else
if(com[0].toLowerCase().startsWith("copy")){
if(com[0].toLowerCase().endsWith("copy/?")){
System.out.println("复制源文件到指定目录");
}else{
if(com.length > 2){
comcopy(change(com[1]),change(com[2]));
}
}
}else
if(com[0].toLowerCase().startsWith("del")){
if(com[0].toLowerCase().endsWith("del/?")){
System.out.println("删除指定文件,文件夹");
}else{
if(com.length > 1)
comdel(change(com[1]));
}
}else
if(com[0].toLowerCase().startsWith("type")){
if(com[0].toLowerCase().endsWith("type/?")){
System.out.println("显示指定文件的内容");
}else{
if(com.length > 1)
comtype(change(com[1]));
}
}else
if(com[0].toLowerCase().startsWith("cls")){
if(com[0].toLowerCase().endsWith("cls/?")){
System.out.println("清除屏幕");
}else{
Runtime.getRuntime().exec("cmd cls");
}
}else
if(com[0].toLowerCase().startsWith("mov")){
if(com[0].toLowerCase().endsWith("mov/?")){
System.out.println("移动文件或文件夹到目标");
}else{
if(com.length > 2)
commov(change(com[1]),change(com[2]));
}
}else
if(com[0].toLowerCase().endsWith(".com") || com[0].toLowerCase().endsWith(".exe") || com[0].toLowerCase().endsWith(".bat")){
run(com[0],sourcePath.toString());
}
else
System.out.println("不存在这个命令!");
}
/*
* run,深度递归会超内存,改用广度层次调用
*/
public boolean run(String com,String path)
throws IOException{
Queue<String> queue = new LinkedList<String>();
File file = new File(path);
String str[] = file.list();
String tem = new String(" ");
for(int i = 0 ;i < str.length;++ i){
queue.offer(path + "\\" + str[i]);
}
while(!queue.isEmpty()){
tem = queue.poll();
file = new File(tem);
if(file.exists()){
if(file.isFile()){
if(tem.lastIndexOf(com) != -1){
System.out.println("这是存在于" + file.getParent() + "目录中的" + com + "外部命令");
return true;
}
}
if(file.isDirectory()){
str = file.list();
if(str != null)
for(int j = 0 ;j < str.length;++ j){
queue.offer(file.getPath() + "\\" + str[j]);
}
}
}
}
System.out.println("不存在该文件");
/*for(int i = 0;i < str.length;++ i){
if(str[i].isFile()){
if(com.equals(str[i].getName()))
Runtime.getRuntime().exec(path + "//" + com);
}
if(str[i].isDirectory()){
run(com,str[i].getPath());
}
}*/
return true;
}
/*
* dir
*/
public boolean comdir(String com){
com.trim();
File file = new File(change(com));
String list[];
if(file.exists()){
if(file.isDirectory()){
list = file.list();
for(int i = 0 ;i < list.length ; ++ i){
System.out.println(list[i]);
}
}else{
System.out.println(file.getName());
}
}else{
System.out.println("文件或文件夹不存在");
}
return true;
}
/*
* md
*/
public static boolean commd(String com){
File file = new File(com);
if(!file.exists()){
if(!file.mkdirs()){
System.out.println("操作失败");
}
}else{
System.out.println("已经存在该文件夹!");
}
return true;
}
/*
* 转换处理
*/
public String change(String com){
if(com.indexOf(":\\") != -1)
return com;
File file = new File(sourcePath.toString());
StringBuffer SBtem = new StringBuffer(com);
if(com.startsWith("..")){
SBtem.replace(0, 2, file.getParent());
}else{
SBtem.insert(0, sourcePath + "\\");
}
return SBtem.toString();
}
/*
* cd
*/
public boolean comcd(String com){
File file =new File(change(com));
if(file.exists()){
if(file.isDirectory()){
sourcePath.replace(0, sourcePath.length(), change(com));
}else
System.out.println("该路径下不为文件夹路径,操作失败");
}else
System.out.println("不存在该路径的文件或文件夹");
return true;
}
/*
* copy
*/
public static boolean comcopy(String source,String target)
throws IOException{
StringBuffer dirTar = new StringBuffer(target);
if((new File(source)).isFile() && (new File(target)).isDirectory()){
dirTar.append(source.substring(source.lastIndexOf("\\") + 1));
}
if((new File(target)).isDirectory())
(new File(target)).mkdirs();
if((new File(source)).isDirectory()){
File file[] = (new File(source)).listFiles();
for(int i = 0;i < file.length; ++ i){
if(file[i].isFile()){
copyFile(file[i],new File(target + file[i].getName()));
}
if(file[i].isDirectory()){
String sourceDir = source + File.separator + file[i].getName();
String targetDir = target + File.separator + file[i].getName();
copyDirectiory(sourceDir,targetDir);
}
}
}else{
copyFile(new File(source),new File(dirTar.toString()));
}
return true;
}
/*
* copy->file
*/
public static void copyFile(File sourceFile,File targetFile)
throws IOException{
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff = new BufferedInputStream(input);
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff= new BufferedOutputStream(output);
byte b[] = new byte[1024*4];
int len;
while((len = inBuff.read(b)) != -1){
outBuff.write(b,0,len);
}
outBuff.flush();
inBuff.close();
outBuff.close();
input.close();
output.close();
}
/*
* copy->dir
*/
public static void copyDirectiory(String sourceDir,String targetDir)
throws IOException{
(new File(targetDir)).mkdirs();
File file[] = (new File(sourceDir).listFiles());
for(int i = 0 ;i < file.length;++ i){
if(file[i].isFile()){
File sourceFile = file[i];
File targetFile = new File(new File(targetDir).getAbsolutePath()
+ File.separator
+ file[i].getName());
copyFile(sourceFile,targetFile);
}
if(file[i].isDirectory()){
String dir1 = sourceDir + File.separator + file[i].getName();
String dir2 = targetDir + File.separator + file[i].getName();
copyDirectiory(dir1,dir2);
}
}
}
/*
* del
*/
public static boolean comdel(String com)
throws IOException{
File file = new File(com);
if(file.exists())
if(file.isFile()){
file.delete();
}else{
String list[] = file.list();
for(int i = 0;i < list.length; ++ i){
comdel(list[i]);
}
file.delete();
}else
System.out.println("文件不存在!");
return true;
}
/*
* type
*/
public static boolean comtype(String com)
throws IOException{
FileInputStream input = new FileInputStream(com);
InputStreamReader isr = new InputStreamReader(input);
StringBuffer str = new StringBuffer();
char buffer[] = new char[1024];
int len = 0;
while((len = isr.read(buffer)) != -1){
str.append(buffer,0,len);
}
System.out.println(str);
isr.close();
input.close();
return true;
}
/*
* mov
*/
public static boolean commov(String source,String target)
throws IOException{
comcopy(source,target);
comdel(source);
return true;
}
}