• 【文件编码转换】将GBK编码项目转为UTF-8编码项目


    需求

    因原项目是GBK编码的,现需要使用UTF-8编码。将项目导入UTF-8编码的编辑器后,出现中文乱码。

    调研

    VsCode 可以转文件编码,但只能一个一个转,对于已经完成一起的项目,操作难免比较麻烦。

    几经周转,特写一个程序,来完成文件编码问题

    能力设计

    根据需求程序被设计为能对原项目完全拷贝并完成转码

    • 能对输入文件、文件夹。若输入文件,则对该文件进行转码;如输入文件夹,则对文件夹中所有的文件进行编码。
    • 能根据文件类型进行过滤处理
    • 能对输入文件夹进行目录结构完全copy。

    以下是将 GBK 编码的 originDir 项目 转化为 UTF-8编码的 destDir 项目的程序示例。

    主要程序

    
    import java.io.File;
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.util.List;
    
    public class Main {
    
    	// 需要转码处理的文件后缀
        public static String[] arr = {
                "java","xml","js",
                "project","MF","css",
                "ftl","properties","ini",
                "product"
        };
    
    	// 原文件夹
        public static String inputDirPath = "/tmp/originDir";
        // 转码后存储的文件夹
        public static String outputDirPath = "/tmp/destDir";
    
        public static void main(String[] args) {
            run(arr,inputDirPath,outputDirPath);
        }
    
        private static void run(String[] arr, String inputDirPath, String outputDirPath) {
    
            File inputFile = new File(inputDirPath);
            if(inputFile.isFile()){
                // 文件处理
                String destFilePath = outputDirPath.concat(File.separator).concat(inputFile.getName());
                File destFile = new File(destFilePath);
                fileHandle(inputFile,destFile);
                return;
            }
            // 获取所有文件及文件夹
            List<File> allFiles = FileUtil.getAllFiles(inputDirPath);
            if(allFiles == null || allFiles.isEmpty()){
                System.out.println("input dir is empty");
                return;
            }
            // 判断是不是文件夹
            for (File file: allFiles){
                File destFile = buildDestFile(inputDirPath, outputDirPath, file);
                if(file.isDirectory()){
                    // 文件夹处理
                    dirHandle(destFile);
    
                }else{
                    // 文件处理
                    fileHandle(file, destFile);
                }
            }
    
    
    
    
        }
    
        private static boolean handleAble(File file, String[] arr) {
            String fileName = file.getName();
            int index = fileName.lastIndexOf(".");
            if(index == -1){
                System.out.println("文件:".concat(file.getAbsolutePath()).concat("不必处理"));
                return false;
            }
            String extName = fileName.substring(index + 1);
            for(String t: arr){
                if(t.equals(extName)){
                    return true;
                }
            }
            return false;
        }
    
        /**
         * 数据处理
         * @param file
         * @param destFile
         */
        private static void fileHandle(File file, File destFile) {
            if(!file.isFile()){
                return;
            }
            System.out.println("开始处理文件:" + file.getAbsolutePath());
            System.out.println("目标文件路径为:" + destFile.getAbsolutePath());
            System.out.println("");
            boolean handleAble = handleAble(file,arr);
            if(!handleAble){
                moveFile(file,destFile);
            }else{
                convertFileEncoding(file, destFile);
            }
        }
    
        /**
         * move file
         * @param file
         * @param destFile
         */
        private static void moveFile(File file, File destFile) {
            try {
                byte[] bytes = Files.readAllBytes(file.toPath());
                Files.write(destFile.toPath(), bytes);
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException("文件处理失败[move],待处理的文件:" + file.getAbsolutePath());
            }
        }
    
        /**
         * change file encoding and save in other place
         * @param file
         * @param destFile
         */
        private static void convertFileEncoding(File file, File destFile) {
            try {
                // 读取文件
    
                List<String> lines ;
                try {
                    lines = Files.readAllLines(file.toPath(), Charset.forName("GBK"));
                }catch (Exception e){
                    lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
                }
                // 转码
                if(lines.isEmpty()){
                    return;
                }
                // 写到新的文件中
                Files.write(destFile.toPath(),lines, StandardCharsets.UTF_8);
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException("文件处理失败,待处理的文件:" + file.getAbsolutePath());
            }
        }
    
        /**
         * 文件夹处理
         * @param destDir 目标文件夹
         */
        private static void dirHandle(File destDir) {
            if(destDir.exists()){
                return;
            }
            boolean mkdirs = destDir.mkdirs();
            if(!mkdirs){
                throw new RuntimeException("file create failed!\r\n File Path : " + destDir.getPath());
            }
        }
    
        /**
         * 构建目标路径
         * @param inputDirPath
         * @param outputDirPath
         * @param file
         * @return
         */
        private static File buildDestFile(String inputDirPath, String outputDirPath, File file) {
            String filePath = file.getAbsolutePath();
            String subFilepath = filePath.substring(inputDirPath.length());
            String destFilePath = outputDirPath.concat(subFilepath);
            return new File(destFilePath);
        }
    }
    
    
    • 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

    工具类

    FileUtil 代码如下:

    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    public class FileUtil {
        /**
         *  

    获取指定文件夹下所有文件,含文件夹

    * @param dirFilePath 文件夹路径 * @return */
    public static List<File> getAllFiles(String dirFilePath){ if(dirFilePath == null || dirFilePath.isEmpty()) return null; return getAllFiles(new File(dirFilePath)); } /** *

    获取指定文件夹下所有文件,不含文件夹

    * @param dirFile 文件夹 * @return */
    public static List<File> getAllFiles(File dirFile){ // 如果文件夹不存在或着不是文件夹,则返回 null if(dirFile == null || !dirFile.exists() || dirFile.isFile()) return null; File[] childrenFiles = dirFile.listFiles(); if(childrenFiles == null|| childrenFiles.length == 0) return null; List<File> files = new ArrayList<>(); for(File childFile : childrenFiles) { // 如果时文件,直接添加到结果集合 if(childFile.isFile()) { files.add(childFile); }else { // 如果是文件夹。则先将其添加到结果集合,再将其内部文件添加进结果集合。 files.add(childFile); List<File> cFiles = getAllFiles(childFile); if(cFiles == null || cFiles.isEmpty()) continue; files.addAll(cFiles); } } return files; } }
    • 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

    总结

    该程序解决了编码转换的需求,经过整理,它有如下优点:

    • 可以自由配置输入文件夹输出文件夹。如果输入文件是一个文件,则对该文件进行编码,并存储到输出文件夹中
    • 可以修改编码满足其他编码方式的转变,如将 UTF-8 转为 GBK
    • 可以配置需要转换的文件路径,细致化控制

    运行环境: JDK 1.8 +

  • 相关阅读:
    【C++庖丁解牛】类与对象
    基于Python医学院校二手书管理毕业设计-附源码201704
    UDS入门至精通系列:Service 27
    NX二次开发-使用SendMessage给NX窗口发送消息最小化
    uva 11729 Commando War(贪心算法)
    MySQl数据库知识点
    flink-sql所有语法详解-1.16
    牛客网刷题篇
    深入理解Vue3.js响应式系统设计之调度执行
    定时器实现原理——时间轮
  • 原文地址:https://blog.csdn.net/m0_47406832/article/details/127463617