• 文件操作工具类


    文件操作工具类

    删除文件及子文件,当 includeSelfDir=true,删除文件夹自身(见代码样例)

    文件重命名(见代码样例)

    文件复制(见代码样例)

    文件更换目录(见代码样例)

    新建文件(见代码样例)

    新建一级目录(见代码样例)

    新建多级目录(见代码样例)

    获取所有文件名-是否包含子文件(见代码样例)

    获取所有文件绝对路径-是否包含子文件(见代码样例)

    获取所有文件绝对路径-是否包含子文件(见代码样例)

    代码样例

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.StandardCopyOption;
    import java.util.List;
    import java.util.Objects;
    
    import com.google.common.collect.Lists;
    import lombok.experimental.UtilityClass;
    import org.apache.commons.collections4.ListUtils;
    import org.junit.platform.commons.util.Preconditions;
    import org.junit.platform.commons.util.StringUtils;
    
    /**
     * @Title:文件操作工具类
     * @Description:
     * @Copyright: Copyright(c)2022
     * @Company: bestcxx
     * @Author: wujie
     * @Date:2022-12-04
     * @Updatedby:
     */
    @UtilityClass
    public class FileUtils {
    
        public static void main(String[] args) throws IOException {
    
    
            String path2 = "/Users/aaa/Desktop/mkdirtest";
            String path3 = "/Users/aaa/Desktop/mkdirtest2/sub";
            mkdir(path2);
            mkdir(path3);
            String path4 = "/Users/aaa/Desktop/mkdirtest/sub1/";
            String path5 = "/Users/aaa/Desktop/mkdirtest/sub2/sub21/sub31";
            mkdirs(path4);
            mkdirs(path5);
    
            System.out.println(getDerictFileNameByPath(path3));
            System.out.println(getAllFileNameByPath(path3));
            System.out.println(createFile(path4 + "/sub1/", "b.word"));
            System.out.println(moveFile(path4 + "/sub1/b.word", path4 + "/sub1/g.word"));
            System.out.println(copyFile(path4 + "/sub1/g.word", path4 + "/sub1/h.word"));
            System.out.println(renameFile(path4 + "/sub1/h.word", path4 + "/sub1/h2.word"));
            String path6 = "/Users/aaa/Desktop/mkdirtest";
            System.out.println(deleteFileOrAllSubFile(path6,true));
    
        }
    
        /**
         * 删除文件及子文件,当 includeSelfDir=true,删除文件夹自身
         *
         * @param originPath 根文件夹
         * @param file
         * @return
         * @throws IOException
         */
        private boolean deleteFileOrSubFile(String originPath,File file,boolean includeSelfDir) throws IOException {
            // 待删除目录不能以 / 结尾
            Preconditions.condition(originPath.lastIndexOf("/")+1 != originPath.length(),"待删除目录结尾不能以 / 结尾");
    
            if (file.isFile()) {
                String tem = file.getAbsolutePath();
                if(file.delete()){
                    System.out.println(String.format("%s 被删除",tem));
                }
            } else if (file.isDirectory()) {
                for (File subFile : file.listFiles()) {
                    deleteFileOrSubFile(originPath,subFile,includeSelfDir);
                }
                String tem = file.getAbsolutePath();
                if(originPath.equals(tem)){
                    if(includeSelfDir && file.delete()){
                        System.out.println(String.format("%s 入参根目录被删除",tem));
                    }
                }else if(file.delete()){
                    System.out.println(String.format("%s 被删除",tem));
                }
            }
            return true;
        }
    
        /**
         * 删除文件及子文件,当 includeSelfDir=true,删除文件夹自身
         *
         * @param fullPath
         * @return
         * @throws IOException
         */
        public boolean deleteFileOrAllSubFile(String fullPath,boolean includeSelfDir) throws IOException {
            return deleteFileOrSubFile(fullPath,new File(fullPath),includeSelfDir);
        }
    
        public boolean renameFile(String fullPathFrom, String fullPathTarget) throws IOException {
            File fileFrom = new File(fullPathFrom);
            File fileTarget = new File(fullPathTarget);
            return fileFrom.renameTo(fileTarget);
        }
    
        public boolean copyFile(String fullPathFrom, String fullPathTarget) throws IOException {
            File fileFrom = new File(fullPathFrom);
            File fileTarget = new File(fullPathTarget);
            Files.copy(fileFrom.toPath(), fileTarget.toPath(), StandardCopyOption.REPLACE_EXISTING);
            return true;
        }
    
        public boolean moveFile(String fullPathFrom, String fullPathTarget) throws IOException {
            File fileFrom = new File(fullPathFrom);
            File fileTarget = new File(fullPathTarget);
            Files.move(fileFrom.toPath(), fileTarget.toPath(), StandardCopyOption.REPLACE_EXISTING);
            return true;
        }
    
        public boolean createFile(String prePath, String fileName) throws IOException {
            String targatFilePath = prePath + fileName;
            File targetFile = new File(targatFilePath);
            if (targetFile.isFile()) {
                System.out.println(String.format("%s 目标文件已存在 ", targatFilePath));
                return true;
            }
    
            File parentPath = new File(prePath);
            if (parentPath.isDirectory()) {
                System.out.println(String.format("%s 层级目录结构已存在 ", prePath));
            } else {
                System.out.println(String.format("层级目录结构初始化 %s ", prePath));
                mkdirs(prePath);
            }
            new FileWriter(prePath + fileName).close();
            System.out.println(String.format("目标文件初始化 %s", targatFilePath));
    
            return true;
        }
    
        /**
         * 新建单级目录
         *
         * @param path
         * @return
         */
        public boolean mkdir(String path) {
            File file = new File(path);
            if (StringUtils.isNotBlank(file.getParent())) {
                return file.mkdir();
            }
            return false;
        }
    
        /**
         * 新建多级目录
         *
         * @param path
         * @return
         */
        public boolean mkdirs(String path) {
            File file = new File(path);
            return file.mkdirs();
        }
    
        public List<String> getDerictFileNameByPath(String path) {
            File file = new File(path);
            if (!file.exists()) {
                return Lists.newArrayList();
            }
            return getFileNames(file, Lists.newArrayList(), true);
    
        }
    
        public List<String> getAllFileNameByPath(String path) {
            File file = new File(path);
            if (!file.exists()) {
                return Lists.newArrayList();
            }
            return getFileNames(file, Lists.newArrayList(), false);
    
        }
    
        public List<String> getAbsoluteFileNameByPath(String path) {
            File file = new File(path);
            if (!file.exists()) {
                return Lists.newArrayList();
            }
            return getAbsoluteFileName(file, Lists.newArrayList(), false);
    
        }
    
        public List<File> getAllFilesByPath(String path) {
            File file = new File(path);
            if (!file.exists()) {
                return Lists.newArrayList();
            }
            return getAllFiles(file, Lists.newArrayList(), false);
    
        }
    
        /**
         * 默认不返回 隐藏文件
         *
         * @param file
         * @param fileNames
         * @param onlyDerictFile
         * @return
         */
        public List<String> getFileNames(File file, List<String> fileNames, boolean onlyDerictFile) {
            if (Objects.isNull(file)) {
                return ListUtils.emptyIfNull(fileNames);
            }
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory() && !onlyDerictFile) {
                    getFileNames(f, fileNames, onlyDerictFile);
                } else if (f.isFile() && !f.isHidden()) {
                    //不要隐藏文件
                    fileNames.add(f.getName());
                }
            }
            return fileNames;
        }
    
        /**
         * 默认不返回 隐藏文件
         *
         * @param file
         * @param allFiles
         * @param onlyDerictFile
         * @return
         */
        public List<String> getAbsoluteFileName(File file, List<String> allFiles, boolean onlyDerictFile) {
            if (Objects.isNull(file)) {
                return ListUtils.emptyIfNull(allFiles);
            }
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory() && !onlyDerictFile) {
                    getAbsoluteFileName(f, allFiles, onlyDerictFile);
                } else if (f.isFile() && !f.isHidden()) {
                    //不要隐藏文件
                    allFiles.add(f.getAbsolutePath());
                }
            }
            return allFiles;
        }
    
        /**
         * 默认不返回 隐藏文件
         *
         * @param file
         * @param allFiles
         * @param onlyDerictFile
         * @return
         */
        public List<File> getAllFiles(File file, List<File> allFiles, boolean onlyDerictFile) {
            if (Objects.isNull(file)) {
                return ListUtils.emptyIfNull(allFiles);
            }
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory() && !onlyDerictFile) {
                    getAllFiles(f, allFiles, onlyDerictFile);
                } else if (f.isFile() && !f.isHidden()) {
                    //不要隐藏文件
                    allFiles.add(f);
                }
            }
            return allFiles;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
  • 相关阅读:
    01-JVM 内存结构
    linux内核分析:进程通讯方式
    HTML+CSS+JS大作业:生态环境网站设计——环境保护主题 大学生环保主题网页制
    煤矿皮带撕裂检测系统
    拓展:Microsoft密钥类型说明
    源码解析:面试必问的LinkedList,看这篇文章就够了
    4. SQL语法中的一些基本概念
    【leetcode】1137. 第 N 个泰波那契数
    ​【Java】面向对象程序设计 课程笔记 面向对象基础
    接口测试之Mock 测试
  • 原文地址:https://blog.csdn.net/bestcxx/article/details/128175982