javadbf中文问题的解决

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

设了几个断点之后跟踪发现是Utils中的textPadding方法有错,原来的方法是
Java代码  收藏代码
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; 
  } 

我改为了
Java代码  收藏代码
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; 
    } 

中文输出完全正常了。

猜你喜欢

转载自zslsh4.iteye.com/blog/2301067