demo

做一个控制台版的文件管理器,要求:

2.1 创建文件/文件夹
2.2 删除文件/文件夹
2.3 复制文件
2.4 移动文件
2.5 重命名文件/文件夹
Files类

package com.gxa.day21.homework.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * 用来处理文件/文件夹的增删改
 * @author:WWA
 * @date:Created at 2020/12/28
 */
public class FileUtil {

    /**
     *  基本的路径
     */
    private String basePath;

    public FileUtil(){
        basePath = "D:\\worksapce_for_idea\\java246\\src\\com\\gxa\\day21\\homework";
    }

    public FileUtil(String basePath){
        this.basePath = basePath;
    }


    /**
     * 创建文件
     * @param path : 文件夹路径
     * @param fileName : 文件名
     * @return
     */
    public boolean createFile(String path, String fileName){
        try {
            if (path == null || path.isEmpty() || fileName == null || fileName.isEmpty()) {
                return false;
            }
            // 将路径构建成File对象
            File dir = new File(path);
            if (!dir.exists()){
                dir.mkdirs();
            }
            // 判断是否是文件夹
            if (!dir.isDirectory()) {
                return false;
            }

            // 构建文件的File对象
            File file = new File(dir, fileName);
            if (file.exists()) {
                return false;
            }

            return file.createNewFile();
        } catch (IOException e) {
            // e.printStackTrace();
            return false;
        }
    }


    /**
     * 创建文件夹的方法
     * @param path : 路径
     * @param dirName : 文件夹名字
     * @return
     */
    public boolean createDir(String path, String dirName){

        if (path == null || path.isEmpty() || dirName == null || dirName.isEmpty()) {
            return false;
        }
        // 将路径构建成File对象
        File dir = new File(path);
        if (!dir.exists()){
            dir.mkdirs();
        }
        // 判断是否是文件夹
        if (!dir.isDirectory()) {
            return false;
        }

        // 构建文件夹的File对象
        File file = new File(dir, dirName);
        if (file.exists()) {
            return false;
        }

        return file.mkdirs();

    }


    /**
     * 删除文件/文件夹
     * @param path : 路径
     * @param fileName : 文件名/文件夹名字
     * @return
     */
    public boolean deleteFile(String path, String fileName){
        if (path == null || path.isEmpty() || fileName == null || fileName.isEmpty()) {
            return false;
        }
        // 构建path的文件对象
        File dir = new File(path);
        if (!dir.exists()) {
            return false;
        }
        // 判断path是否是文件夹
        if (!dir.isDirectory()) {
            return false;
        }
        // 构建需要删除的文件对象
        File file = new File(dir, fileName);
        // 判断是否存在
        if (!file.exists()) {
            return false;
        }
        // 判断是否是文件
        if (file.isFile()) {
            // 是文件就直接删除
            file.delete();
        }
        // 判断是否是文件夹
        if (file.isDirectory()) {
            // 获取下面的所有的文件列表
            String[] list = file.list();
            // 遍历
            for (String fileStr : list) {
                // 直接递归
                deleteFile(file.getPath(), fileStr);
            }

            // 删除外层的文件夹
            file.delete();
        }
        return true;
    }


    /**
     * 复制单个文件
     * @param source : 源文件的路径
     * @param target : 目标文件的路径
     * @return
     */
    public boolean copyFile(String source, String target){
        try {
            // static Path copy(Path source, Path target, CopyOption... options)
            // 构建源文件的File对象
            File srcFile = new File(source);
            if (!srcFile.exists()) {
                return false;
            }
            // 构建目标文件的file对象
            File targetFile = new File(target);
            if (targetFile.exists()) {
                return false;
            }

            Files.copy(srcFile.toPath(), targetFile.toPath());

            return true;
        } catch (IOException e) {
            // e.printStackTrace();
            return false;
        }
    }


    /**
     * 移动文件
     * @param source
     * @param target
     * @return
     */
    public boolean moveFile(String source, String target){
        try {
            // static Path copy(Path source, Path target, CopyOption... options)
            // 构建源文件的File对象
            File srcFile = new File(source);
            if (!srcFile.exists()) {
                return false;
            }
            // 构建目标文件的file对象
            File targetFile = new File(target);
            if (targetFile.exists()) {
                return false;
            }

            Files.move(srcFile.toPath(), targetFile.toPath());

            return true;
        } catch (IOException e) {
            // e.printStackTrace();
            return false;
        }
    }




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值