Java项目编码GBK转换UTF8

依赖jar

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
public static void main(String[] args) {
        //GBK编码格式源码路径
        String srcDirPath = "D:\\Users\\IdeaWorkSpace\\gitee";
        //转为UTF-8编码格式源码路径
        String utf8DirPath = "D:\\Users\\IdeaWorkSpace\\gitee\\UTF8";
        //获取所有java文件
        Collection<File> javaGbkFileCol =  FileUtils.listFiles(new File(srcDirPath), new String[]{"java"}, true);
        for (File javaGbkFile : javaGbkFileCol) {
            //UTF8格式文件路径
            String utf8FilePath = utf8DirPath+javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
            //使用GBK读取数据,然后用UTF-8写入数据
            try {
                FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/u014653854/article/details/81173253