All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.FileUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import matrix.boot.common.exception.ServiceException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 文件工具,文件夹工具
 *
 * @author wangcheng
 * 2021/8/10
 **/
public class FileUtil {

    /**
     * 创建新文件,如果存在先删除
     *
     * @param file 文件
     */
    public static void createNewFile(File file) {
        try {
            if (file.exists() && !file.delete()) {
                throw new ServiceException("file delete failed!");
            }
            if (!file.createNewFile()) {
                throw new ServiceException("file create failed!");
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 删除文件
     *
     * @param file 文件
     */
    public static void removeFile(File file) {
        if (file.exists() && !file.delete()) {
            throw new ServiceException("file delete failed!");
        }
    }

    /**
     * 创建文件夹
     *
     * @param paths 多个文件夹地址
     */
    public static void mkdirs(String... paths) {
        for (String path : paths) {
            FileUtil.mkdirs(path);
        }
    }

    /**
     * 创建文件夹
     *
     * @param path 单个文件夹地址
     */
    private static void mkdirs(String path) {
        AssertUtil.notNullTip(path, "path");
        path = path.replace(File.separator, "&");
        String[] folders = path.split("&");
        StringBuilder folderPath = new StringBuilder();
        File file;
        for (String folder : folders) {
            folderPath.append(folder).append(File.separator);
            file = new File(folderPath.toString());
            if (!file.exists() && !file.mkdirs()) {
                throw new ServiceException("folder create fail");
            }
        }
    }

    /**
     * 递归文件夹中得文件
     *
     * @param folderPath 文件夹目录
     * @param files      文件列表
     */
    public static void getFileList(File folderPath, List files) {
        if (folderPath != null && Objects.requireNonNull(folderPath.list()).length > 0) {
            File[] listFiles = folderPath.listFiles();
            if (listFiles == null || listFiles.length <= 0) {
                return;
            }
            for (File file : listFiles) {
                files.add(file);
                if (file.isDirectory()) {
                    FileUtil.getFileList(file, files);
                }
            }
        }
    }

    /**
     * 删除文件夹
     *
     * @param folderPath 文件夹地址
     */
    public static void rmdir(String folderPath) {
        List files = new ArrayList<>();
        File folderFile = new File(folderPath);
        FileUtil.getFileList(folderFile, files);
        if (files.size() > 0) {
            files.forEach(FileUtil::removeFile);
        }
        removeFile(folderFile);
    }

    /**
     * 获取文件名,去除扩展名
     *
     * @param fileName 文件名称
     * @return 文件名
     */
    public static String getPrefix(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    /**
     * 获取后缀名
     *
     * @param fileName 文件名称
     * @return 文件后缀名
     */
    public static String getSuffix(String fileName) {
        int index = fileName.lastIndexOf(".");
        if (index >= 0) {
            return fileName.substring(index);
        }
        return "";
    }

    /**
     * 获取java缓存文件夹
     *
     * @return String
     */
    public static String getTmpDir() {
        return System.getProperty("java.io.tmpdir");
    }

    /**
     * 获取相对路径
     *
     * @param realPath 真实路径
     * @return 文件路径地址
     */
    public static String getPositionPath(String realPath) {
        String result;
        URL fileUrl;
        fileUrl = FileUtil.class.getResource(realPath);
        fileUrl = fileUrl != null ? fileUrl : (FileUtil.class.getResource("/"));
        fileUrl = fileUrl != null ? fileUrl : (FileUtil.class.getClassLoader().getResource(realPath));
        fileUrl = fileUrl != null ? fileUrl : (FileUtil.class.getClassLoader().getResource("/"));
        if (fileUrl != null) {
            result = fileUrl.getPath();
            if (!result.contains(realPath)) {
                result = result.substring(0, result.length() - 1) + realPath;
            }
            File fileFolder = new File(result);
            if (!fileFolder.exists()) {
                FileUtil.mkdirs(fileFolder.getAbsolutePath());
            }
        } else {
            result = getAbsolutePath(realPath);
        }
        return result;
    }

    /**
     * 获取绝对路径
     *
     * @param realPath 真实路径
     * @return 文件路径
     */
    public static String getAbsolutePath(String realPath) {
        String result;
        try {
            Process process = Runtime.getRuntime().exec("pwd");
            InputStreamReader isr = new InputStreamReader(process.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            String sysPath = br.readLine();
            br.close();
            isr.close();
            process.destroy();
            result = sysPath + realPath;
        } catch (IOException e) {
            result = "C:" + realPath.replace("/", "\\");
        }
        File fileFolder = new File(result);
        if (!fileFolder.exists()) {
            FileUtil.mkdirs(fileFolder.getAbsolutePath());
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy