Java初级黄金体验 其二

Java初级黄金体验 其二

引言:让我们看一下你的C盘有多少个文件和文件夹

初级 Java IO : 第一个纪念碑

小程序大致功能

让我们看一下E盘有多少个文件

上代码

最近太多的作业

代码可以无限改进,君子回头十年不晚,先写软工去

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("*删除失败!");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/Martrix-revolution/p/11681666.html