`
ivan
  • 浏览: 179341 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

javadbf中文问题的解决

    博客分类:
  • java
阅读更多
前面shuyanxu朋友提到javadbf(http://fireshort.blogbus.com/logs/2005/09/1420670.html
)的中文支持问题,由于我测试得不够仔细就忽略掉了。最近发现读取中文是没有问题的,但写入dbf的时候就会产生乱码。

设了几个断点之后跟踪发现是Utils中的textPadding方法有错,原来的方法是
 public static byte[] textPadding( String text, String characterSetName, int length, int alignment,
   byte paddingByte) throws java.io.UnsupportedEncodingException {
      if( text.length() >= length) {
         return text.substring( 0, length).getBytes( characterSetName);
      }

      byte byte_array[] = new byte[ length];
      Arrays.fill( byte_array, paddingByte);

      switch( alignment) {
         case ALIGN_LEFT:
            System.arraycopy( text.getBytes( characterSetName), 0, byte_array, 0, text.length());
            break;

         case ALIGN_RIGHT:
            int t_offset = length - text.length();
            System.arraycopy( text.getBytes( characterSetName), 0, byte_array, t_offset, text.length());
            break;
         }   
      return byte_array;
   }

我改为了
public static byte[] textPadding(String text,String characterSetName,
            int length,int alignment,byte paddingByte)
            throws java.io.UnsupportedEncodingException
    {
        byte[] srcByteArray=text.getBytes(characterSetName);
        byte[] dstByteArray=new byte[length];
        Arrays.fill(dstByteArray,paddingByte);

        int dstLength=0;
        if(srcByteArray.length>=length)
        {
            dstLength=length%2==0?length:length-1;
        }else
        {
            dstLength=srcByteArray.length;
        }

        switch(alignment)
        {

        case ALIGN_LEFT:            System.arraycopy(srcByteArray,0,dstByteArray,0,dstLength);
            break;

        case ALIGN_RIGHT:
            System.arraycopy(srcByteArray,0,dstByteArray,length-dstLength,dstLength);
            break;
        }
        return dstByteArray;
    }

中文输出完全正常了。

附件是打过补丁后的javadbf.jar。
7
2
分享到:
评论
5 楼 mengqingyu 2013-03-07  
列头乱码咋解决?
4 楼 tianlihu 2013-01-04  
问题解决,谢谢
3 楼 giianhui 2012-12-11  
luo2pei4321 写道
在读取文件时还是会出现乱码。

reader.setCharactersetName("GB2312");
2 楼 luo2pei4321 2012-11-15  
在读取文件时还是会出现乱码。
1 楼 hannaje 2010-04-06  
 
困扰我很久了……

相关推荐

Global site tag (gtag.js) - Google Analytics