将文件夹下面的文本文件转换格式

package com.itester.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class ConvertGBKToUTF8 {

    public static void main(String[] args) {
        try {
            List resultFile = new ArrayList();
            File dfile = new File("C:\\Users\\Administrator\\Desktop\\sql\\龙元的SQL\\emmmm");
            ConvertGBKToUTF8.getDirectoryFiles(dfile, resultFile);
            System.out.println("文件数量:" + resultFile.size());
            for (int i = 0; i < resultFile.size(); i++) {
                transferFile(resultFile.get(i));
                System.out.println("成功转换【" + i+1 + "】个文件!");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void getDirectoryFiles(File file, List resultFile) {
        if (file.isDirectory()) {
            File[] fileArr = file.listFiles();
            for (File itemFile : fileArr) {
                getDirectoryFiles(itemFile, resultFile);
            }
        } else if (file.isFile()) {
            resultFile.add(file);
        }
    }

    public static void transferFile(File orgFile) throws Exception {
        String orgFileContent = FileUtils.readFileToString(orgFile, "UTF-8");
        FileUtils.writeStringToFile(orgFile, orgFileContent, "GBK");
    }

}

 

转载于:https://www.cnblogs.com/xzp0222/p/8744051.html

你可能感兴趣的:(将文件夹下面的文本文件转换格式)