使用Java读写dbf文件【附源代码】

📢📢📢📣📣📣

哈喽!大家好,今天给大家分享一篇如何使用Java代码解析DBF文件,获取数据。于是自己封装了一个工具类,读取DBF文件中的数据以及字段值,将读取到的数据封装成对象进行返回。

✨个人博客:https://blog.csdn.net/weixin_43759352✨

✨公众号:【SimpleMemory】✨

❤️❤️❤️如果有对【后端技术】感兴趣的大佬们,欢迎关注!!!❤️❤️❤️

主要代码如下:

pom.xml文件中引入所需依赖;
 

<dependency>
     <groupId>com.github.albfernandez</groupId>
     <artifactId>javadbf</artifactId>
     <version>1.9.2</version>
</dependency>

创建返回实体类 DBFInfo.java​​​​​​​

/**
 * @author 公众号: SimpleMemory
 * @version 1.0.0
 * @ClassName DBFInfo.java
 * @Description DBF文件封装实体类
 * @createTime 2022年02月26日 20:37:00
 */
@Data
public class DBFInfo {
    // 字段数量
    private Integer fieldCount;
    // 字段名
    private String[] fieldName;
    // 记录
    List<Map<String, String>> rowList;
    //记录数量
    Integer recordCount;
}

核心实现类

/**
 * @author 公众号: SimpleMemory
 * @version 1.0.0
 * @ClassName DBFUtils.java
 * @Description 读取DBF文件工具类
 * @createTime 2022年02月26日 20:27:00
 */
@Slf4j
public class DBFUtils {
    /**
     * 读取DBF文件
     *
     * @param filePath    文件路径
     * @param charsetName
     * @return
     * @throws IOException
     */
    public static DBFInfo readDBFFile(String filePath, String charsetName) throws IOException {
        DBFInfo dbfInfo = new DBFInfo();
        FileInputStream inputStream = null;
        DBFReader dbfReader = null;
        Object[] rowVales = null;
        List<Map<String, String>> rowList = new ArrayList<>();
        File file = new File(filePath);
        if (!file.exists()) {
            log.error("文件路径:{},该文件不存在!", filePath);
            return dbfInfo;
        }
        try {
            log.debug("开始读取DBF文件,文件路径为: {}", filePath);
            inputStream = new FileInputStream(filePath);
            dbfReader = new DBFReader(inputStream, Charset.forName(charsetName), false);

            // 字段数量
            int fieldCount = dbfReader.getFieldCount();
            log.debug("读取DBF文件,字段数量为:{}个!", fieldCount);

            // 记录数量
            int recordCount = dbfReader.getRecordCount();
            log.debug("读取DBF文件,数据量为:{}个!", recordCount);


            String[] fieldName = new String[fieldCount];
            for (int i = 0; i < fieldCount; i++) {
                fieldName[i] = dbfReader.getField(i).getName();
            }
            dbfInfo.setFieldCount(fieldCount);
            dbfInfo.setFieldName(fieldName);
            dbfInfo.setRecordCount(recordCount);

            while ((rowVales = dbfReader.nextRecord()) != null) {
                Map<String, String> rowMap = new HashMap<String, String>();
                for (int i = 0; i < rowVales.length; i++) {
                    rowMap.put(dbfReader.getField(i).getName(), String.valueOf(rowVales[i]).trim());
                }
                rowList.add(rowMap);
            }
            if (CollectionUtils.isNotEmpty(rowList)) {
                dbfInfo.setRowList(rowList);
            }
            return dbfInfo;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                inputStream.close();
            }
            if (null != dbfReader) {
                dbfReader.close();
            }
        }
        return dbfInfo;
    }

如果这篇【文章】对您有帮助,希望大家点赞、评论、关注、收藏;如果对【后端技术】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 公众号【SimpleMemory】❤️❤️❤️,将会继续给大家带来【收获与惊喜】💕💕!

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值