解决java-dbf中文标题乱码的问题

项目中需要对DBF的文件进行导入处理,上网搜了发现有java-dbf这个东西。实际应用中踩了不少坑,主要就是中文乱码的问题。

        InputStream in = new FileInputStream("D:\\test.dbf");
        DBFReader reader = new DBFReader(in);
        reader.setCharactersetName("GBK");
        for(int i = 0; i < reader.getFieldCount(); i++){
            DBFField field = reader.getField(i);
            System.out.print(field.getName() + ",");
        }
        System.out.print("\r\n");
        Object[] values;
        while ( (values = reader.nextRecord()) != null ){
            for(Object value : values){
                System.out.print(value.toString() + ",");
            }
            System.out.print("\r\n");
        }

网上写法千篇一律,大概就是这样。问题来了DBF中具体数据的中文乱码通过reader.setCharactersetName("GBK")解决了。

 

但是发现列名的乱码还是没有解决。

 

后来查了一下源码,发现了问题所在

    public DBFReader(InputStream in, Charset charset, boolean showDeletedRows) {
        try {
            this.showDeletedRows = showDeletedRows;
            this.inputStream = in;
            this.dataInputStream = new DataInputStream(this.inputStream);
            this.header = new DBFHeader();
            this.header.read(this.dataInputStream, charset, showDeletedRows);
            setCharset(this.header.getUsedCharset());
            /* it might be required to leap to the start of records at times */
            int fieldSize = this.header.getFieldDescriptorSize();
            int tableSize = this.header.getTableHeaderSize();
            int t_dataStartIndex = this.header.headerLength - (tableSize + (fieldSize * this.header.fieldArray.length)) - 1;            
            skip(t_dataStartIndex);
            
            this.mapFieldNames = createMapFieldNames(this.header.userFieldArray);
        } catch (IOException e) {
            DBFUtils.close(dataInputStream);
            DBFUtils.close(in);
            throw new DBFException(e.getMessage(), e);
        }
    }

其中header就是我们读取的列名,列数所依靠的成员变量,但是这个变量在对象创建的时候就赋值好了。

这就导致了后来即使调用了setCharactersetName也解决不了列名乱码问题。

所以我们要从根本上解决问题,创建对象的时候直接传入charset对象。

 

修改后代码如下

    public static void main(String[] args) throws FileNotFoundException {
        InputStream in = new FileInputStream("D:\\test.dbf");
        Charset charset = Charset.forName("GBK");
        DBFReader reader = new DBFReader(in,charset);
        for(int i = 0; i < reader.getFieldCount(); i++){
            DBFField field = reader.getField(i);
            System.out.print(field.getName() + ",");
        }
        System.out.print("\r\n");
        Object[] values;
        while ( (values = reader.nextRecord()) != null ){
            for(Object value : values){
                System.out.print(value.toString() + ",");
            }
            System.out.print("\r\n");
        }
    }

 

输出时候列名就正常了


转载于:https://www.cnblogs.com/guofx/p/11509472.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值