• 2005年10月19日

    javadbf中文问题的解决

    Tag:DB j2se 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;
        }

    中文输出完全正常了。

  • 2005年09月08日

    操作dbf的类库

    Tag:DB Java j2se
    操作DBF文件的类库建议采用javadbf。

    名称为javadbf的开源项目有两个,一个是
    http://javadbf.dev.java.net,因为这个项目缺少文档而代码注释不知是何国鸟语,故不选;另一个是http://sarovar.org/projects/javadbf/,印度人的开源项目,文档完备,注释多到冗余,在http://sarovar.org上下载次数排第6,LGPL。

    通过对一些dbf文件进行读写测试,印度人的这个项目完全符合我们项目的需要。我们将采用此javadbf作为操作dbf的工具。
  •  "When java tries to deserialize an object, it compares the serialized object's serialVersionUID with that of the class the JVM  is using for deserializing the object. ...
    If the two numbers don't match, the JVM assumes the class is not compatible with the previously-serialized object, and you'll get an exception during deserialization.
    So, the solution is to put a serialVersionUID in your class, and then as the class evolves, the serialVersionUID will remain the same and the JVM will say, "Ok, cool, the class is compatible with this serialized object." even though the class has actually changes"

    If the class has been changed and you want the JVM to not to load the previously serialized classes then the serialVersionUID . Otherwise leave it alone.