• 2005年10月19日

    javadbf中文问题的解决

    Tag:DB j2se Java

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://fireshort.blogbus.com/logs/1518064.html

    前面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;
        }

    中文输出完全正常了。


    收藏到:Del.icio.us




    引用地址:

    引用

    下面Blog引用了该文:

    评论

  • 急用。。问题解决。。谢谢了。。
  • 楼主厉害,按照楼主的代码这个问题迎刃而解,多谢楼主了
  • 请教下我用的时候读出的字段内容都有错误,遇到过吗。如原来字段叫做中华人民共和国,现在中华在一个字段里,后面的和其他的在一个字段里。

发表评论

您将收到博主的回复邮件
记住我