java导出dbf文件生僻汉字处理
java导出数据到dbf文件,如果姓名中有生僻汉字,在dbf中看到的很可能是?号。
遇到这种情况需查对GBK的生僻汉字的Unicode表,GBK提及的52个生僻汉字有两种Unicode。
例如:
䶮(yan 3) \u4ADE就不能在dbf中正常显示是?
如果换成\uE863则可以(可以打开word的插入->符号->其他符号,在字符代码中输入4ADE的到字符插入word,输入E863的到另一形式插入word,将这两种形式的字符从word拷贝到Visual Fox Pro的命令窗口可以看到差别,一个变成?一个能正常显示)。
解决方式:
1.建立52个生僻汉字的unicode映射Map
2.将生僻汉字转成unicode形式(有可能是将整个姓名转成unicode)
3.将姓名的unicode形式进行分割(\u)生成数组(注意两端的双引号)
4.遍历unicode数组,如果找到生僻汉字的unicode则进行替换
5.将unicode还原成汉字
6.写入dbf
汉字转unicode可利用(import com.alibaba.fastjson.JSON) :
//unicode转中文 public static String unicodeToString ( String str ) { return String . valueOf ( JSON . parse ( str )); } //中文字符转unicode public static String stringToUnicode ( String s ) { return JSON . toJSONString ( s , SerializerFeature . BrowserCompatible ); }其他说明:
例如:
䶮在mysql中能显示出来,导出到dbf中时如果选择 字符集为 GB2312或GBK,导出的 䶮为?。
在Visual Fox Pro 9的命令窗口里输入的 䶮为?
打开word,插入,输入字符编码4DAE得到 䶮,插入到word,复制粘贴到 Visual Fox Pro 9的命令窗口改字显示 为?
打开word,插入,输入字符编码8E63得到 ,有些版本的Word能显示出来,有些版本的不能显示,按Alt+X ,插入到word,复制粘贴到 Visual Fox Pro 9的命令窗口改字能显示 正常
上图输入E863无反应
按快捷键Alt+x后的效果
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://blog.csdn.net/jbgtwang/article/details/103662530
查看更多关于java导出dbf文件生僻汉字处理方式的详细内容...