javadbf中文问题的解决

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

设了几个断点之后跟踪发现是Utils中的textPadding方法有错,原来的方法是
[code]
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;
}
[/code]
我改为了
[code]
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;
}
[/code]
中文输出完全正常了。

附件是打过补丁后的javadbf.jar。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javadbf-0.4.0版本本已增加setCharactersetName方法支持中文读写,但在写入时会发生写入中文数据丢失问题。资源压缩文件包含有修改后的jar文件和读写测试类。 @Test public void testWrite1(){ File file=new File(this.folder, "测试数据.DBF"); OutputStream fos = null; try { // 定义DBF文件字段 DBFField[] fields = new DBFField[3]; // 分别定义各个字段信息,setFieldName和setName作用相同, // 只是setFieldName已经不建议使用 fields[0] = new DBFField(); // fields[0].setFieldName("emp_code"); fields[0].setName("semp_code"); fields[0].setDataType(DBFField.FIELD_TYPE_C); fields[0].setFieldLength(10); fields[1] = new DBFField(); // fields[1].setFieldName("emp_name"); fields[1].setName("emp_name"); fields[1].setDataType(DBFField.FIELD_TYPE_C); fields[1].setFieldLength(200); fields[2] = new DBFField(); // fields[2].setFieldName("salary"); fields[2].setName("salary"); fields[2].setDataType(DBFField.FIELD_TYPE_N); fields[2].setFieldLength(12); fields[2].setDecimalCount(2); // DBFWriter writer = new DBFWriter(new File(path)); // 定义DBFWriter实例用来写DBF文件 DBFWriter writer = new DBFWriter(); writer.setCharactersetName("GBK"); // 把字段信息写入DBFWriter实例,即定义表结构 writer.setFields(fields); // 一条条的写入记录 Object[] rowData = new Object[3]; rowData[0] = "1000234567"; // "中文";// -42 -48 -50 -60 rowData[1] = "中文"; rowData[2] = new Double(5000.00); writer.addRecord(rowData); rowData = new Object[3]; rowData[0] = "1001"; rowData[1] = "Lalit"; rowData[2] = new Double(3400.00); writer.addRecord(rowData); rowData = new Object[3]; rowData[0] = "1002"; rowData[1] = "Rohit"; rowData[2] = new Double(7350.00); writer.addRecord(rowData); // 定义输出流,并关联的一个文件 fos = new FileOutputStream(file); // 写入数据 /* * 注意:writer.addRecord(rowData)时并不真正写入数据,在最后writer.write(fos)时才会把数据写入DBF文件,之前addRecord的数据暂时存放在内存中。如果数据量过大,这种方式显然不适合. */ writer.write(fos); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception e) { } } } /** * JavaDBF提供的另外一种机制:Sync Mode(同步模式)解决数据量过大这个问题 */ @Test public void testWrite2(){ File file=new File(this.folder, "测试数据.DBF"); DBFWriter writer = null; try { if(file.exists()){ file.delete(); } // 定义DBF文件字段 DBFField[] fields = new DBFField[3]; // 分别定义各个字段信息,setFieldName和setName作用相同, // 只是setFieldName已经不建议使用 fields[0] = new DBFField(); // fields[0].setFieldName("emp_code"); fields[0].setName("semp_code"); fields[0].setDataType(DBFField.FIELD_TYPE_C); fields[0].setFieldLength(10); fields[1] = new DBFField(); // fields[1].setFieldName("emp_name"); fields[1].setName("emp_name"); fields[1].setDataType(DBFField.FIELD_TYPE_C); fields[1].setFieldLength(200); fields[2] = new DBFField(); // fields[2].setFieldName("salary"); fields[2].setName("salary"); fields[2].setDataType(DBFField.FIELD_TYPE_N); fields[2].setFieldLength(12); fields[2].setDecimalCount(2); // DBFWriter writer = new DBFWriter(new File(path)); // 定义DBFWriter实例用来写DBF文件 writer = new DBFWriter(file); writer.setCharactersetName("GBK"); // 把字段信息写入DBFWriter实例,即定义表结构 writer.setFields(fields); // 一条条的写入记录 Object[] rowData = new Object[3]; rowData[0] = "1000234567"; rowData[1] = "中文1"; rowData[2] = new Double(5000.00); writer.addRecord(rowData); rowData = new Object[3]; rowData[0] = "1001"; rowData[1] = "Lalit"; rowData[2] = new Double(3400.00); writer.addRecord(rowData); rowData = new Object[3]; rowData[0] = "1002"; rowData[1] = "Rohit"; rowData[2] = new Double(7350.00); writer.addRecord(rowData); } catch (Exception e) { e.printStackTrace(); } finally { if(writer!=null){ try { writer.write(); } catch (Exception e) {} } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值