DosTool.java
package cn.dong.demo02;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
public class DosTool {
/**
* Dos的工具类
*/
//显示目录下的文件或文件夹
public static void dir(){
Scanner.printf("请输入要查找的目录名:");
String s = Scanner.getKey();
File file = new File(s);
File[] files = file.listFiles();
for (File f : files) {
if(f.isDirectory()){
System.out.println(f.getAbsolutePath()+"<DIR>");
}
if(f.isFile()){
System.out.println(f.getAbsolutePath()+",文件大小为:"+f.length()/1024.0+"KB");
}
}
}
//创建文件夹
public static void md(){
Scanner.printf("请输入要创建的目录名:");
String s = Scanner.getKey();
File file = new File(s);
if(file.exists()){
System.out.println("该目录已经存在");
}else{
boolean b = file.mkdirs();
if(b){
System.out.println("创建成功");
}else{
System.out.println("创建失败");
}
}
}
//显示时间
public static void time(){
String ss = DateFormat.getTimeInstance().format(new Date());
System.out.println(ss);
}
//显示日期
public static void date(){
String ss = DateFormat.getDateInstance().format(new Date());
System.out.println(ss);
}
//对文件重命名
public static void rename(){
Scanner.printf("请输入重名命名的文件:");
String sFile = Scanner.getKey();
Scanner.printf("请输入修改后的文件名:");
String dFile = Scanner.getKey();
File f1 = new File(sFile);
File f2 = new File(dFile);
boolean b = f1.renameTo(f2);
if(b){
System.out.println("改名成功");
}else{
System.out.println("改名失败");
}
}
//复制文件
public static void copy(){
Scanner.printf("请输入复制文件的位置:");
String s = Scanner.getKey();
Scanner.printf("请输入复制后的位置:");
String d = Scanner.getKey();
try {
FileInputStream fis = new FileInputStream(s);
FileOutputStream fos = new FileOutputStream(d);
byte[] bs = new byte[1024];
int c = 0;
while((c=fis.read(bs))!=-1){
fos.write(bs,0,c);
}
fis.close();
fos.close();
System.out.println("复制成功");
} catch (Exception e) {
e.printStackTrace();
}
}
//退出
public static void exit(){
System.exit(0);
}
//运行程序
public static void run(){
Scanner.printf("请输入运行程序的命令:");
String command = Scanner.getKey();
Runtime r = Runtime.getRuntime();
try {
r.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
//删除文件或文件夹
public static void del(){
Scanner.printf("请输入要删除的文件:");
String s = Scanner.getKey();
File file = new File(s);
boolean b = file.delete();
if(b){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}
Scanner.java
package cn.dong.demo02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Scanner {
/**
* 键盘输入的方法
*/
public static String getKey() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
/**
* 输入的方法
* @param s
*/
public static void printf(String s){
System.out.println(s);
}
}
JavaDos.java
package cn.dong.demo02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 用java实现Dos下的命令
*/
public class JavaDos {
/**
* 运行dos命令
*/
public static void runDos(){
Scanner.printf("------------欢迎来到dos界面--------------");
while(true){
Scanner.printf("请输入dos命令:");
String s = Scanner.getKey();
if(s.equalsIgnoreCase("dir")){
DosTool.dir();
}else if(s.equalsIgnoreCase("md")){
DosTool.md();
}else if(s.equalsIgnoreCase("time")){
DosTool.time();
}else if(s.equalsIgnoreCase("date")){
DosTool.date();
}else if(s.equalsIgnoreCase("rename")){
DosTool.rename();
}else if(s.equalsIgnoreCase("exit")){
break;
}else if (s.equalsIgnoreCase("copy")){
DosTool.copy();
}else if(s.equalsIgnoreCase("run")){
DosTool.run();
}else if(s.equalsIgnoreCase("del")){
DosTool.del();
}else{
Scanner.printf("输入了非法命令");
}
}
Scanner.printf("谢谢使用");
}
public static void main(String[] args) {
runDos();
}
}
运行结果: