vtd解析xml文件 gbk编码格式 解析不成功解决方法

我记得官网文档上说过是不支持gbk格式的,所以只能转码了。

直接上一个转码得工具类

package com.roof.datahandle.fileread;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @PackageName:PACKAGE_NAME
 * @ClassName:convertFile
 * @Description:文件编码转换
 * @Author: sunhongchen
 * @create 2018-12-14 11:04
 */
public class ConvertFile {
    /**
     * @param sourceFileRoot 将要转换文件所在的根目录或文件路径
     * @param sourceCharset  源文件编码
     * @param targetCharset  目标文件编码
     * @Author:SunHongchen
     * @Description
     * @Date: 2018/12/14 11:05
     * @Method ConvertFile
     * @Return void
     */
    public static void convertFile(String sourceFileRoot, String sourceCharset, String targetCharset)
            throws IOException {
        if (isUTF8File(sourceFileRoot)) {
            return;
        }
        File fileDir = new File(sourceFileRoot);
        convert(fileDir, sourceCharset, targetCharset);
    }

    private static boolean isUTF8File(String filePath) {
        boolean isUTF = false;

        File f = new File(filePath);
        try {
            java.io.InputStream ios = new java.io.FileInputStream(f);
            byte[] b = new byte[3];
            ios.read(b);
            ios.close();
            if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
                System.out.println(f.getName() + "编码为UTF-8");
                isUTF = true;
            } else {
                System.out.println(f.getName() + "可能是GBK");
                isUTF = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isUTF;
    }

    private 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("<?xml version=\"1.0\" encoding = \""+targetCharset+"\"?>\r\n");
            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();
        } else {
            //利用递归对目录下的每个以.xml结尾的文件进行编码转换
            for (File subFile : file.listFiles()) {
                convert(subFile, sourceCharset, targetCharset);
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值