文件编码转化 GBK 转 UTF-8工具类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @PackageName:PACKAGE_NAME
 * @ClassName:convertFile
 * @Description:文件编码转换
 * @create 2018-12-14 11:04
 */
public class convertFile
{
    private static int fileCount = 0;

    private static Logger log = LoggerFactory.getLogger(convertFile.class);

    /**
     * @param sourceFileRoot 将要转换文件所在的根目录或文件路径
     * @param sourceCharset  源文件编码
     * @param targetCharset  目标文件编码
     * @Description
     * @Date: 2018/12/14 11:05
     * @Method convertFile
     * @Return void
     */
    public static void convertFile(String sourceFileRoot, String sourceCharset, String targetCharset)
        throws IOException
    {
        File fileDir = new File(sourceFileRoot);
        convert(fileDir, sourceCharset, targetCharset);
        log.info("Total Dealed : " + fileCount + "Files");
    }

    public static void convert(File file, String sourceCharset, String targetCharset)
        throws IOException
    {
        // 如果是文件则进行编码转换,写入覆盖原文件
        if (file.isFile())
        {
            // 只处理.xml结尾的代码文件
            if (file.getPath().indexOf(".xml") == -1)
            {
                return;
            }
            int i = 1;
            InputStreamReader isr = new InputStreamReader(new FileInputStream(
                file), sourceCharset);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null)
            {
                //对文件第一行进行操作
                if (i == 1)
                {
                    //判断是否是已经转换过得文件
                    if (line.toLowerCase().contains(targetCharset.toLowerCase()))
                    {
                        br.close();
                        isr.close();
                        return;
                    }
                    line = line.replaceAll("(?i)" + sourceCharset, targetCharset);
                }
                // 注意写入换行符
                line = URLEncoder.encode(line, "utf8");
                sb.append(line + "\r\n");//windows 平台下 换行符为 \r\n

                i++;
            }

            br.close();
            isr.close();

            File targetFile = new File(file.getPath());
            OutputStreamWriter osw = new OutputStreamWriter(
                new FileOutputStream(targetFile), targetCharset);
            BufferedWriter bw = new BufferedWriter(osw);
            // 以字符串的形式一次性写入
            bw.write(URLDecoder.decode(sb.toString(), "utf8"));
            bw.close();
            osw.close();

            //  System.out.println("Deal:" + file.getPath());
            fileCount++;
        }
        else
        {
            //利用递归对目录下的每个以.xml结尾的文件进行编码转换
            for (File subFile : file.listFiles())
            {
                convert(subFile, sourceCharset, targetCharset);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28773851/article/details/85002146