File工具类

package com.leng.demo.utils;

import java.io.*;

/*
 * FileUtil:提供了文件操作相关方法,例如创建文件、读写文件、复制文件等
 * */
public final class FileUtil {
    private FileUtil() {
    }

    /**
     * 创建文件,如果父目录不存在,会自动创建
     *
     * @param fileName 文件名(含路径)
     * @return 是否创建成功
     */
    public static boolean createFile(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            return true;
        }
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        try {
            return file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 读取文本文件内容
     *
     * @param fileName 文件名(含路径)
     * @param charset  字符集
     * @return 返回文件内容字符串
     */
    public static String readFileToString(String fileName, String charset) {
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), charset))) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 将字符串写入到文本文件中
     *
     * @param fileName 文件名(含路径)
     * @param content  待写入的字符串
     * @param charset  字符集
     * @return 是否写入成功
     */
    public static boolean writeStringToFile(String fileName, String content, String charset) {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), charset))) {
            writer.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 复制文件
     *
     * @param source 源文件路径
     * @param target 目标文件路径
     * @return 是否复制成功
     */
    public static boolean copyFile(String source, String target) {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target))) {
            byte[] buf = new byte[1024];
            int length;
            while ((length = in.read(buf)) > 0) {
                out.write(buf, 0, length);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除指定目录下(含子目录)和文件
     *
     * @param folderPath 要删除的目录路径
     * @return 是否删除成功
     */
    public static boolean deleteFolder(String folderPath) {
        File file = new File(folderPath);
        if (!file.exists()) {
            return true;
        }
        if (file.isFile()) {
            return file.delete();
        }
        if (!file.isDirectory()) {
            return false;
        }
        for (File f : file.listFiles()) {
            if (f.isFile()) {
                closeStream(f);
                if (!f.delete()) {
                    return false;
                }
            } else {
                if (!deleteFolder(f.getAbsolutePath())) {
                    return false;
                }
            }
        }
        closeStream(file);
        return file.delete();
    }

    private static void closeStream(File file) {
        try {
            if (file.isDirectory()) {
                return;
            }
            FileInputStream inputStream = new FileInputStream(file);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值