java读写dbf文件

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java中解析DBF文件可以使用Apache Commons IO和Apache Commons Lang库。以下是一个简单的示例代码,演示了如何读取DBF文件并输出其内容: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.time.DateUtils; public class DBFReader { private String fileName; private int headerLength; private int recordLength; public DBFReader(String fileName) { this.fileName = fileName; } public void read() throws IOException { FileInputStream inputStream = new FileInputStream(fileName); byte[] headerBytes = new byte[32]; IOUtils.read(inputStream, headerBytes); headerLength = NumberUtils.toInt(StringUtils.substring(headerBytes, 8, 10)); recordLength = NumberUtils.toInt(StringUtils.substring(headerBytes, 10, 12)); byte[] recordBytes = new byte[recordLength]; while (IOUtils.read(inputStream, recordBytes) != -1) { String recordString = new String(recordBytes, "ISO-8859-1"); System.out.println(recordString); } } public static void main(String[] args) throws Exception { DBFReader reader = new DBFReader("example.dbf"); reader.read(); } } ``` 这个示例代码使用了Apache Commons IO和Apache Commons Lang库来读取DBF文件。它首先读取文件头,然后使用记录长度来读取每个记录。在每个记录中,它将字节数组转换为字符串,并将其输出到控制台。请注意,DBF文件使用ISO-8859-1编码,因此我们需要使用该编码将字节数组转换为字符串。此外,我们还需要使用Apache Commons Lang库来解析整数和日期值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值