在控制台模擬操作cmd 我們設計簡單的程式實現以下功能 1.cd顯示當前目錄的名稱或將其更改。 2.date顯示時間 3.md 創建一個目錄 4. rd 刪除目錄 5.dir 顯示一個目錄中的文件和子目錄 6 help 提示操作 代碼 先在項目下創建一個help.txt文件,內容從cmd的help中 ...
在控制台模擬操作cmd
我們設計簡單的程式實現以下功能
1.cd顯示當前目錄的名稱或將其更改。
2.date顯示時間
3.md 創建一個目錄
4. rd 刪除目錄
5.dir 顯示一個目錄中的文件和子目錄
6 help 提示操作
代碼
先在項目下創建一個help.txt文件,內容從cmd的help中拷貝。
package com.nll.io; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class Cmd { public static void main(String[] args) { System.out.println("************************************"); System.out.println("****************nll*****************************"); System.out.println("****************@2020****************"); System.out.println("*************************************"); //先來顯示我們有哪些盤 showRoot(); //我們給一個預設的路徑 我們預設的是第一個盤 String path=File.listRoots()[0].getPath(); Scanner sc=new Scanner(System.in); //開始迴圈的來輸入指令 while(true) { //首先 先展示 System.out.print(path+">");//這個>沒有其他意思,主要是為了和cmd一致 String cmd=sc.nextLine(); //開始判斷指令 if("help".equalsIgnoreCase(cmd)) { help(); }else if(cmd.endsWith(":")) { path=changeDisk(cmd,path); System.out.println(); }else if("dir".equalsIgnoreCase(cmd)) { dir(path); }else if(cmd.startsWith("cd ")) { path=cd(cmd,path); }else if(cmd.startsWith("copy ")) { copy(cmd,path); }else if(cmd.startsWith("del ")) { del(cmd,path); }else if(cmd.startsWith("md ")){ md(cmd,path); }else if(cmd.startsWith("rd")) { rd(cmd,path); }else if(cmd.equals("date")) { Calendar c=Calendar.getInstance(); Date d=new Date(); int i=(c.get(Calendar.DAY_OF_WEEK))-1; SimpleDateFormat sdf=new SimpleDateFormat("YYYY/MM/dd"+"\t"+(i==0?"日":"i")); System.out.println("當前日期:"+sdf.format(d)); }else { System.out.println("'"+cmd+"'不是內部或外部命令,也不是可運行的程式\n"); } } } private static void rd(String cmd, String path) { // TODO Auto-generated method stub String c=cmd.substring(3).trim(); File dir=new File(path+"\\"+c); deleleAll(dir); System.out.println("刪除成功!"); } private static void deleleAll(File f) { // TODO Auto-generated method stub // TODO Auto-generated method stub //首先判斷 你這個f是文件還是目錄 if(f.isFile()) { //如果是文件 則直接刪除 f.delete(); return; } //目錄 則獲取到所有文件 File[] fs=f.listFiles(); for(File file:fs) { deleleAll(file);//遞歸自己調用自己 } f.delete();//刪除目錄 } private static void md(String cmd, String path) { // TODO Auto-generated method stub String c=cmd.substring(3).trim(); File dir=new File(path+"\\"+c); if(!dir.exists()) { dir.mkdir(); System.out.println(c+"創建成功"); }else { System.out.println("創建失敗"); } } private static void del(String cmd, String path) { // TODO Auto-generated method stub String c=cmd.substring(4).trim(); File f=new File(path,c); if(f.exists()&&f.isFile()) { File f2=new File(path+"\\"+c); f2.delete(); }else { System.out.println("此文件不存在或不可刪除"); } } private static void copy(String cmd, String path) { // TODO Auto-generated method stub String [] arr=cmd.split(" "); File f1=new File(path,arr[1]); File f2=new File(path,arr[2]); if(f1.exists()&&f2.exists()&&f1.isFile()&&f2.isFile()) { copy1(arr[1],arr[2],path); } } private static void copy1(String a1, String a2, String path) { // TODO Auto-generated method stub File f1=new File(path+"\\"+a1); File f2=new File(path+"\\"+a2); try { BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f1)); byte [] b=new byte[bis.available()]; bis.read(b); bis.close(); OutputStream os=new FileOutputStream(f2); os.write(b); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static String cd(String cmd, String path) { String newPath=cmd.substring(3).trim(); if(".".equalsIgnoreCase(newPath)) { return path; }else if("..".equalsIgnoreCase(newPath)) { //返回上一級 File f=new File(path); String parents=f.getParent(); if(parents!=null) { return parents; }else { return path; } }else { //切換文件夾 File file=new File(path,newPath); if(file.exists()&&file.isDirectory()) { return file.getPath(); }else { //沒有 System.out.print("系統找不到指定的路徑。\n"); return path; } } } private static void dir(String path) { // TODO Auto-generated method stub //先根據當前目錄 來獲取到所有的子文件 File file=new File(path); File [] fs=file.listFiles(); int fnum=0; int dnum=0; System.out.print("\n"); for(File f:fs) { long time=f.lastModified(); Date d=new Date(time); SimpleDateFormat sdf=new SimpleDateFormat("YYYY年MM月dd日 HH:mm"); System.out.print(sdf.format(d)); //接著判斷是否是目錄 if(f.isDirectory()) { dnum++; System.out.print("\t目錄\t\t"); }else { fnum++; if(f.length()>10000000) { System.out.print("\t\t"+f.length()); }else { System.out.print("\t\t"+f.length()+"\t"); } } System.out.print("\t"+f.getName()+"\n"); } System.out.print("\n總共"+fnum+"個文件,總共"+dnum+"個目錄\n"); } private static String changeDisk(String cmd, String path) { if(new File(cmd).exists()) { return cmd.toUpperCase()+"\\"; }else { System.out.print("系統找不到指定的驅動器。\n"); return path; } } //help指令的方法 private static void help() { // TODO Auto-generated method stub try { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("help.txt")); byte[] b =new byte[bis.available()]; bis.read(b); //讀出來之後顯示 String str=new String(b); System.out.println(str+"\n"); //流使用完之後 避免浪費記憶體 記得關閉流 bis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void showRoot() { // TODO Auto-generated method stub File[] fs=File.listRoots(); for(File f:fs) { System.out.print(f.getPath()+"\t"); } System.out.println("\n\n"); } }
效果如下