import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class CMDDemo01 {
private static String [] cmd={"dir","copy","del","type","comp","help"};
public static void main(String[] args) {
System.out.println("关于dos命令及其作用");
System.out.println("1.dir 列文件名");
System.out.println("2.copy 拷贝文件");
System.out.println("3.del 删除文件");
System.out.println("4.type 显示文件内容");
System.out.println("5.comp比较两个或两套文件的内容");
System.out.println("6.help [命令] 或 ?[命令] 查看命令说明");
System.out.println("7.exit 退出cmd.exe程序或目前");
//从键盘上获取一个字符串
Scanner sc=new Scanner(System.in);
while (true){
System.out.print(":");
String s=sc.nextLine();
//判断输入的是否为exit
if(s.equals("exit")){
System.exit(0);
}
//将字符串分割成字符数组
String[] split = s.split(" ");
int n=findCmd(split[0]);
if(n!=-1) {
switch (n) {
case 0:
doDir(split);
break;
case 1:
doCopy(split);
break;
case 2:
doDel(split);
break;
case 3:
doType(split);
break;
case 4:
doComp(split);
break;
case 5:
doHelp();
break;
default:
}
}else {
System.out.println("您输入的命令暂不支持");
}
}
}
public static int findCmd(String s){
int index=-1;
for(int i=0;it");
System.out.println("t"+listName[i]);
}
}
}else {
count++;
Date d1=new Date(file.lastModified());
System.out.print(sdf.format(d1));
System.out.print("tt");
System.out.println(file.getName());
}
System.out.println("文件共有"+count+"个");
}
//copy 拷贝文件
public static void doCopy(String [] s){
int index=0;
if(s.length!=3){
System.out.println("该命令格式输入错误");
index=0;
}else {
//获取想要复制文件的目的地和复制文件的路径
// Path path1 = Paths.get(s[1]);
// Path path2 = Paths.get(s[2]);
// //用Files中的复制方法来复制文件
// try {
// Files.copy(path1,path2, StandardCopyOption.REPLACE_EXISTING);
// }catch(IOException e){
// e.printStackTrace();
// }
//根据键盘输入的文件地址创建File对象
File f1=new File(s[1]);
File f2=new File(s[2]);
//用字节流来复制文件(夹)
try {
copyFolder(f1,f2);
} catch (IOException e) {
e.printStackTrace();
}
index=1;
}
if(index==1) {
System.out.println("文件复制成功");
}else {
System.out.println("文件复制失败!");
}
}
//复制文件的方法(通过字节缓冲输入流来实现文件的复制)
private static void copyFile(File srcFile,File destFile)throws IOException {
//创建字节缓冲输入流,读数据
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
//创建字符缓冲输出流写数据
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));
//读取数据(一次读取一个字节数组)
byte[]bys=new byte[1024];
int len;
while ((len= bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
//文件夹的复制(由于文件夹里有其他东西,所以只能用字节流复制,因为字节缓冲流比字节流快,所以用字节缓冲流)
private static void copyFolder(File srcFile, File destFile) throws IOException {
//判断数据源File是否是目录
if(srcFile.isDirectory()) {
//在目的地下创建和数据源File名称一样的目录
String srcFileName = srcFile.getName();
File newFolder = new File(destFile,srcFileName); //F:\itcast
if(!newFolder.exists()) {
newFolder.mkdir();
}
//获取数据源File下所有文件或者目录的File数组
File[] fileArray = srcFile.listFiles();
//遍历该File数组,得到每一个File对象
for(File file : fileArray) {
//把该File作为数据源File对象,递归调用复制文件夹的方法
copyFolder(file,newFolder);
}
} else {
//说明是文件,直接复制,用字节流
File newFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
//del 删除文件
public static void doDel(String [] s){
int index=0;
if(s.length!=2){
System.out.println("请输入正确的格式");
index=0;
}else {
//调用Paths中的get方法获取到文件的绝对路径
Path source=Paths.get(s[1]);
try {
Files.deleteIfExists(source);
}catch(IOException e){
e.printStackTrace();
}
index=1;
}
//判断文件是否删除成功后
if(index==0){
System.out.println("文件删除失败");
}else {
System.out.println("文件删除成功");
}
}
//type 显示文件内容
public static void doType(String [] s) {
BufferedReader br = null;
FileReader fr = null;
if (s.length != 2) {
System.out.println("输入格式错误");
System.out.println("type: 显示文件内容");
} else {
try {
//创建字符缓冲输入流
String line;
fr = new FileReader(s[1]);
br = new BufferedReader(fr);
while ((line=br.readLine())!=null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println();
} catch (IOException ioe) {
System.out.println("请输入文件地址");
} finally {
try {
//释放资源
fr.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//comp比较两个或两套文件的内容
public static void doComp(String [] s){
if(s.length!=3){
System.out.println("请您输入正确的格式");
}else {
//先判断您输入的文件是否存在
//创建两个File对象
File f1=new File(s[1]);
File f2=new File(s[2]);
int index=0;//用来判断是否要比较文件内容
//判断第一个文件是否存在
if(!f1.exists()){
System.out.println("您所输入的文件"+f1.toString()+"不存在");
}
//判断第二个文件是否存在
if(!f2.exists()){
System.out.println("您所输入的文件"+f2.toString()+"不存在");
}
if(f1.exists()&&f2.exists()){//只有两个文件同时存在才能比较其内容
index=1;
}
if(index==1){
try {
//创建两个字节缓冲流,读取两个文件的内容
BufferedInputStream bis1=new BufferedInputStream(new FileInputStream(s[1]));
BufferedInputStream bis2=new BufferedInputStream(new FileInputStream(s[2]));
int n1,n2;
int n=0;
while (((n1=bis1.read())!=-1)&&((n2=bis2.read())!=-1)){//一一比较两个文件中的任何一个字节
if(n1==n2){
n=1;//遍历文件的所有内容
}else {
n=0;//只要有一个字节不一样,文件就不相同
}
}
if(n==1){
System.out.println("文件相同");
}else {
System.out.println("文件不同");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("文件不同");
}
}
}
//help [命令] 或 ?[命令] 查看命令说明
public static void doHelp(){
System.out.println("目前只支持以下的几种命令");
//遍历数组,输出所有命令的内容
for(String s:cmd){
System.out.print(s+" ");
}
System.out.println(" ");
}
}