使用的是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