-
2005年10月19日
javadbf中文问题的解决
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
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;
}
中文输出完全正常了。随机文章:
操作dbf的类库 2005年09月08日数据库连接池的选型:DBCP vs C3P0 2005年07月16日用Java进行LDAP编程的方式 2007年01月23日Java中对有BOM头的UTF-8文件的处理 2006年10月19日
收藏到:Del.icio.us
引用地址:






评论