最近太多的做業java
代碼能夠無限改進,君子回頭十年不晚,先寫軟工去小程序
package com.lxy.io; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; /** * 建立目錄 * @author lxy * * E:\aa\ff */ public class IOt2 { //定義static屬性 static String check_fuc; static String op_name; //定義輸入函數 public static String shuru() { Scanner scanner = new Scanner(System.in); String key = scanner.nextLine(); return key; } public static void main(String[] args) throws IOException { //基礎輸出 System.out.println("*按Q/q進行一次操做||按其餘鍵退出"); String ss1 = shuru(); while(ss1.equals("Q") || ss1.equals("q")) { //功能 System.out.println(" -----------------------------------------"); System.out.println("|建立新文件/文件夾--->1|刪除文件/文件夾--->2|"); System.out.println(" -----------------------------------------"); System.out.println("|查找文件/文件夾----->3|打開文件/文件夾--->4|"); System.out.println(" -----------------------------------------"); check_fuc = shuru(); switch(check_fuc) { case "1" :{ System.out.println(" ---------------------------------"); System.out.println("|建立新文件---->1|建立新文件夾---->2|"); System.out.println(" ---------------------------------"); String check_creat = shuru(); switch(check_creat) { case "1": { System.out.println("*請輸入所要建立的名稱:"); op_name = shuru(); Fifo ff = new Fifo(op_name); ff.creatIt(); break; } case "2" :{ System.out.println("*請輸入所要建立的名稱:"); op_name = shuru(); Directory dr_tmp = new Directory(op_name); dr_tmp.creatIt(); break; } } break; } case "2" :{ System.out.println("*請輸入所要刪除的名稱:"); op_name = shuru(); int tmp = checkName(op_name); if(tmp == 3) { System.out.println("文件不存在"); }else if(tmp == 2) { Fifo ff_tmp = new Fifo(op_name); ff_tmp.deleteIt(); }else { Directory dr_tmp = new Directory(op_name); dr_tmp.deleteIt(); } break; } case "3" :{ System.out.println("*請輸入所要查找的名稱:"); op_name = shuru(); File ff = new File(op_name); if(ff.exists()) { System.out.println(ff.getName()+"存在!"); System.out.println("其位置在:"+ff.getAbsolutePath()); } break; } case "4" :{ System.out.println("*請輸入要打開的文件名稱"); op_name = shuru(); int tmp = checkName(op_name); if(tmp == 3) { System.out.println("文件不存在"); }else if(tmp == 2) { System.out.println(" ---------------------------------"); System.out.println("|查看內容---->1|增添/修改內容---->2|"); System.out.println(" ---------------------------------"); String cont = shuru(); switch(cont) { case "1" :{ viewFifo(op_name); break; } case "2" :{ System.out.println(" ----------------------------"); System.out.println("|增添內容---->1|修改內容---->2|"); System.out.println(" ----------------------------"); String mdf = shuru(); switch(mdf) { case "1" :{ appendFifo(op_name); break; } case "2": { modifyFifo(op_name); break; } } break; } } }else { printDirBasicMsg(op_name); } break; } } //基礎輸出 System.out.println("按Q/q進行一次操做||按其餘鍵退出"); ss1 = shuru(); } System.out.println("你已退出!"); } //封裝文件夾查看功能函數 public static void printDirBasicMsg(String s) { Directory Dr = new Directory(s); System.out.println("該文件夾基本信息以下:"); System.out.println("*大小:"+Dr.getDirLength()+"字節"); System.out.println("*子文件夾數量:"+Dr.getDirChilddirNum()+"個"); System.out.println("*子文件數量:"+Dr.getDirChildfileNum()+"個"); System.out.println("*其子文件名稱以下"); // Dr.printContent(Dr.fd, 0); } //封裝文件查看函數及其修改內容的功能 public static void viewFifo(String s) throws FileNotFoundException { Fifo fo = new Fifo(s); fo.printContent(); } //增長內容 public static void appendFifo(String s){ Fifo fo = new Fifo(s); fo.appendIt(); } //修改內容 public static void modifyFifo(String s){ Fifo fo = new Fifo(s); fo.modifyIt(); } //檢查爲文件夾/文件函數 public static int checkName(String s) { int flag; File f_check = new File(s); if(f_check.isDirectory()) { flag = 1; } else if(f_check.isFile()) { flag = 2; }else { flag = 3; } return flag; } } //文件夾類 class Directory{ // private String path; //文件夾長度 private int dirLength; //全部子文件夾的數量 private int dirChilddirNum; //全部子文件的數量 private int dirChildfileNum; public File fd; // public Directory(String s) { this.path = s; this.fd = new File(path); this.dirLength = 0; this.dirChilddirNum = 0; this.dirChildfileNum = 0; this.openFuc(fd); } //文件夾的大小以及子文件/文件夾的數量 private void openFuc(File fd) { if(fd != null && fd.exists()) { if(fd.isFile()) { this.dirChildfileNum++; dirLength += fd.length(); }else if(fd.isDirectory()) { this.dirChilddirNum++; if(fd.listFiles() != null) for(File f1:fd.listFiles()) openFuc(f1); } } } //列出全部子文件夾和子文件 public void printContent(File f,int deep) { for(int i = 0; i < deep; ++i) { System.out.print("-"); } System.out.println(f.getName()); if(f == null||!f.exists()) { return ; }else if(f.isDirectory()) { if(f.listFiles() != null) for(File f1:f.listFiles()) { printContent(f1,deep+1); } } } //建立文件夾 public void creatIt() { if(fd.mkdirs()) { System.out.println("*建立文件夾成功!"); }else if(fd.exists()) { System.out.println("*文件夾已存在請勿重複建立!"); }else { System.out.println("*建立失敗!"); } } //刪除文件夾 public void deleteIt() { if(fd.delete()) { System.out.println("*文件夾"+fd.getName()+"已刪除!"); }else { System.out.println("*刪除失敗!"); } } public int getDirLength() { return dirLength; } public int getDirChilddirNum() { return dirChilddirNum; } public int getDirChildfileNum() { return dirChildfileNum; } } class Fifo{ private String path; File fdd; FileReader fread; FileWriter fwriter; //構造器 Fifo(String s){ this.path = s; fdd = new File(path); } //查看文件內容 public void printContent() throws FileNotFoundException { System.out.println("*文件已打開,內容以下"); fread = new FileReader(fdd); char[] a = new char[100]; try { fread.read(a); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(char tmp : a) { System.out.print(tmp); } try { fread.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("-------------------------"); } //增添文件內容 public void appendIt() { Scanner scanner_tmp = new Scanner(System.in); System.out.println("*請在下方輸入你要添加的內容"); String tmp = scanner_tmp.nextLine(); try { fwriter = new FileWriter(this.fdd,true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fwriter.write(tmp); fwriter.flush(); fwriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("*文件內容添加成功"); } //修改文件內容 public void modifyIt() { Scanner scanner_tmp = new Scanner(System.in); System.out.println("*請在下方輸入你要修改的內容"); String tmp = scanner_tmp.nextLine(); try { fwriter = new FileWriter(this.fdd,false); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fwriter.write(tmp); fwriter.flush(); fwriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("*文件內容修改爲功"); } //建立文件 public void creatIt() throws IOException { if(fdd.createNewFile()) { System.out.println("*建立新文件成功!"); }else if(fdd.exists()){ System.out.println("*文件"+fdd.getName()+"已存在!"); }else { System.out.println("*建立新文件失敗!"); } } //刪除文件 public void deleteIt() { if(fdd.delete()) { System.out.println("*文件"+fdd.getName()+"已刪除!"); }else { System.out.println("*刪除失敗!"); } } }