使用的是javadbf-0.4.0.jar,javadbf-0[1].4.1.jar
1、jar区别
javadbf-0.4.0.jar为正式表,对中文支持不好,出现丢失中文数据问题。主要是没有考虑中文占2个字节问题。
javadbf-0[1].4.1.jar是在 javadbf-0.4.0.jar基础上修改中文处理bug,能够出来部分中文问题,在字符串包含中文且总长度奇数并且等于dbf字段长度时,会丢失最后一个字节数据。
修改javadbf-0[1].4.1.jar 中Utils.java类 textPadding 方法
public static byte[] textPadding(String text, String characterSetName, int length, int alignment, byte paddingByte)
throws UnsupportedEncodingException
{
byte[] srcByteArray = text.getBytes(characterSetName);
byte[] dstByteArray = new byte[length];
Arrays.fill(dstByteArray, paddingByte);
int dstLength = 0;
if (srcByteArray.length > length) // 原先为if (srcByteArray.length >= length)
dstLength = (length % 2 == 0) ? length : length - 1;
else {
dstLength = srcByteArray.length;
}
switch (alignment)
{
case 10:
System.arraycopy(srcByteArray, 0, dstByteArray, 0, dstLength);
break;
case 12:
System.arraycopy(srcByteArray, 0, dstByteArray, length - dstLength,
dstLength);
case 11:
}
return dstByteArray;
}
2、取数据
type[] temp = new byte[];
InputStream dbfIS = new ByteArrayInputStream(temp);
DBFReader dbfreader = new DBFReader(dbfIS);
dbfreader.setCharactersetName("GBK");
for (int b = 0; b < dbfreader.getFieldCount(); b++) {
DBFField dbfd = dbfreader.getField(b);
dbfd.getDataType(); // dbf数据类型
dbfd.getDecimalCount(); // 小数位
dbfd.getFieldLength(); // 字段总长度
dbfd.getName(); // 字段名称
}
1)、取值注意末尾空字符串,dbf数据为固定长度,取出字符串时会带空字符串。
2)、数据类型:
case DBFField.FIELD_TYPE_C: // 字符串
case DBFField.FIELD_TYPE_M: // 可以用字符串传值
case DBFField.FIELD_TYPE_D: // 时期
case DBFField.FIELD_TYPE_F: // 数值 用Double
case DBFField.FIELD_TYPE_N: // 数值 用Double
case DBFField.FIELD_TYPE_L: // Boolean
3)、Boolean 为Boolean.FALSE;对象
3、存数据
DBFWriter dbfwriter = null;
int colCount = list.size();
DBFField ajdbfield[] = new DBFField[colCount];
//读取列信息,设置新的列信息
for (int b = 0; b < colCount; b++) {
DBFField tempDBFField = ((DbfZbMapping)list.get(b)).getDbfField();
byte dataType = tempDBFField.getDataType();
int decimalCount = tempDBFField.getDecimalCount();
int fieldLength = tempDBFField.getFieldLength();
String name = tempDBFField.getName().trim().toUpperCase();
ajdbfield[b] = new DBFField();
ajdbfield[b].setName(name);
ajdbfield[b].setDataType(dataType);
ajdbfield[b].setFieldLength(fieldLength);
ajdbfield[b].setDecimalCount(decimalCount);
}
dbfwriter = new DBFWriter(outDbfFile); // outDbfFile 为导出文件
dbfwriter.setCharactersetName("GBK");
dbfwriter.setFields(ajdbfield);
return dbfwriter;
Object colObjValueD[] = adjustFieldValue((List)fileMap.get(key1), feb, guids[i],wdp,item.getDbfUnitLog());
if (colObjValueD == null)continue;
dbfwriter.addRecord(colObjValueD);
dbfwriter.write();// 一定要加
1)setName 名称不能重复
2)设置列长要比小数位长大
3)setDataType为
case DBFField.FIELD_TYPE_C: // 字符串
case DBFField.FIELD_TYPE_M: // 可以用字符串传值
case DBFField.FIELD_TYPE_D: // 时期
case DBFField.FIELD_TYPE_F: // 数值 用Double
case DBFField.FIELD_TYPE_N: // 数值 用Double
case DBFField.FIELD_TYPE_L: // Boolean