Java基础案例教程 第七章IO(输入输出)———【任务7-3】模拟文件管理器 ()

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

一、任务描述:

二、运行结果:

 

三、实现思路:

1、循环提示,让用户输入选项

  while (true){
            System.out.print("请输入指令:");
            try{
                command = scanner.nextInt();
                switch(command) {
                    case 1: // 指定关键字检索文件
                        searchByKey();
                        break;
                    case 2: // 指定后缀名检索文件
                        searchBySuffix();
                        break;
                    case 3: // 复制文件/目录
                        copyDirectory();
                        break;
                    case 4: //退出
                        exit();
                        break;
                    default:
                        System.out.println("您输入的命令不合法");
                        break;
                }
            } catch(InputMismatchException e){  //捕获输入不合法
                scanner.next();
                System.out.println("指令必须为数字");
            }

      }

2、指定关键字检索文件:

//根据关键字搜索文件
    private static void searchByKey() {
        System.out.print("请输入要检索的目录位置:");
        Scanner scanner =new Scanner(System.in);
        String  searchPath=scanner.nextLine();
        File searchFilePath=new File(searchPath);
        System.out.print("请输入搜索关键字:");
        String key=scanner.nextLine();
        //调用工具类方法
        FileUtils.searchByKey(searchFilePath, key);
    }

      轮询每个文件,检查文件名是否含有关键字

public class FileUtils {
    //在searchPath这个目录下,搜索出所有含有关键字key的文件
    public  static void searchByKey(File searchPath,String key){
        //把目录下所有文件列出来
        File[] files=searchPath.listFiles();
        for(File file: files){
            if (file.isDirectory()){
                //递归调用
                searchByKey(file, key);
            }else{
                //System.out.println("this file ="+file );
                if(file.getName().contains(key)){
                    System.out.println(file);
                }

            }
        }
    }

3、方法内容较多,新建工具类 FileUnits,内部创建静态方法

    (静态方法可以直接用类名调用,实例方法必须先创建对象实例再调用方法)

4、复制文件夹

1)全部以单个文件形式复制出来

/**
     * @param srcPath 源文件夹  D:\IDEA-IO\src\2019-02
     * @param desPath 目标文件夹 D:\IDEA-IO\des
     */
    public static void copyDirectory(File srcPath, File desPath) throws IOException {
        //目标文件夹不一定存在
        boolean res = desPath.mkdirs();  //mkdirs可建立多级文件     mkdir()只能建立一级文件
        File[] files = srcPath.listFiles();

        for (File f : files) {
            System.out.println(f);
            if (f.isDirectory()) { //是文件夹
                copyDirectory(f, desPath);
            } else {   //是文件
                //传过来的只是目录,这里建立对应的目标文件
                File desFile = new File(desPath, f.getName());
                //使用字节流缓冲区复制文件
                FileInputStream in = new FileInputStream(f);
                FileOutputStream out = new FileOutputStream(desFile);
                //读文件
                byte[] buff = new byte[1024];
                int len;
                while ((len = in.read()) != -1) {
                    //写文件
                    out.write(buff, 0, len);
                }
                //关闭流
                in.close();
                out.close();
            }

        }

    }

2)包含文件夹复制出来

 if (f.isDirectory()) {  //是文件夹
                copyDirectory(f, new File(desPath+"\\"+f.getName()));
            } else { 

四、实现代码:

package cn.itcast.task03;

import cn.itcast.task01.FileUnit;

import java.io.File;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 16:24
 * @describe:    模拟文件管理器
 */
public class DocumentManagement {
    
    
    public static void main(String[] args) throws IOException {
        System.out.println("---1:指定关键字检索文件   2:指定后缀名检索文件   3:复制文件、目录  4:退出  ");
        Scanner scanner = new Scanner(System.in);
        int command;
        
        while (true){
            System.out.print("请输入指令:");
            try{
                command = scanner.nextInt();
                switch(command) {
                    case 1: // 指定关键字检索文件
                        searchByKey();
                        break;
                    case 2: // 指定后缀名检索文件
                        searchBySuffix();
                        break;
                    case 3: // 复制文件/目录
                        copyDirectory();
                        break;
                    case 4: //退出
                        exit();
                        break;
                    default:
                        System.out.println("您输入的命令不合法");
                        break;
                }
            } catch(InputMismatchException e){  //捕获输入不合法
                scanner.next();
                System.out.println("指令必须为数字");
            }

      }
    }

    //根据关键字搜索文件
    private static void searchByKey() {
        System.out.print("请输入要检索的目录位置:");
        Scanner scanner =new Scanner(System.in);
        String  searchPath=scanner.nextLine();
        File searchFilePath=new File(searchPath);
        System.out.print("请输入搜索关键字:");
        String key=scanner.nextLine();
        //调用工具类方法
        FileUtils.searchByKey(searchFilePath, key);
    }

    //以指定后缀名搜索文件
    private static void searchBySuffix() {
        System.out.print("请输入要检索的目录位置:");
        Scanner scanner =new Scanner(System.in);
        String  searchPath=scanner.nextLine();
        File searchFilePath=new File(searchPath);
        System.out.print("请输入搜索后缀:");
        String suffix=scanner.nextLine();
        //调用工具类方法
        FileUtils.searchBySuffix(searchFilePath, suffix);
    }

    // 复制文件/目录
    private static void copyDirectory() throws IOException {
        Scanner scanner =new Scanner(System.in);
        System.out.print("请输入源目录:");
        File srcPath=new File(scanner.nextLine());
        //System.out.println("srcPath = "+srcPath.toString());
        //System.out.println("srcPath = "+srcPath);

        System.out.print("请输入目标位置:");
        File desPath=new File(scanner.nextLine());
        //System.out.println("desPath = "+desPath.toString());
        FileUtils.copyDirectory(srcPath, desPath);
    }

    private static void exit() {
        System.out.println("您已退出系统,谢谢使用");
        System.exit(0);
    }
}
package cn.itcast.task03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 16:46
 * @describe: 工具类
 */

public class FileUtils {
    //在searchPath这个目录下,搜索出所有含有关键字key的文件
    public static void searchByKey(File searchPath, String key) {
        //把目录下所有文件列出来
        File[] files = searchPath.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                searchByKey(file, key);
            } else {
                //System.out.println("this file ="+file );
                if (file.getName().contains(key)) {
                    System.out.println(file);
                }

            }
        }
    }


    //在searchPath这个目录下,搜索出所有以suffix为后缀的文件
    public static void searchBySuffix(File searchPath, String suffix) {
        //把目录下所有文件列出来
        File[] files = searchPath.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                searchBySuffix(file, suffix);
            } else {
                //System.out.println("this file ="+file );
                if (file.getName().endsWith(suffix)) {
                    System.out.println(file);
                }

            }
        }
    }

    /**
     * @param srcPath 源文件夹  D:\IDEA-IO\src\2019-02
     * @param desPath 目标文件夹 D:\IDEA-IO\des
     */
    public static void copyDirectory(File srcPath, File desPath) throws IOException {
        System.out.println("\r\n进入copyDirectory ");
        System.out.println("srcPath = " + srcPath);
        System.out.println("desPath = " + desPath);

        //目标文件夹不一定存在
        boolean res = desPath.mkdirs();  //mkdirs可建立多级文件     mkdir()只能建立一级文件
        System.out.println("desPath.mkdirs() res " + res);
        File[] files = srcPath.listFiles();

        for (File f : files) {
            System.out.println("f= " + f);
            if (f.isDirectory()) {  //是文件夹
               //包含子文件夹复制
                copyDirectory(f, new File(desPath + "\\" + f.getName()));
            } else {        //是文件
                //传过来的只是目录,这里建立对应的目标文件
                File desFile = new File(desPath, f.getName());
                //使用字节流缓冲区复制文件
                FileInputStream in = new FileInputStream(f);
                FileOutputStream out = new FileOutputStream(desFile);
                //读文件
                byte[] buff = new byte[1024];
                int len;
                while ((len = in.read()) != -1) {
                    //写文件
                    out.write(buff, 0, len);
                }
                //关闭流
                in.close();
                out.close();
            }

        }

    }
}

猜你喜欢

转载自blog.csdn.net/wangpailiulanqi8/article/details/94624024