java写DBF文件

之前漏了个功能支持,那就是WhoNet上报的DBF文件导出,因为DBF基本没什么人在用了,实现DbfUtil供业务写DBF文件做WhoNet上报导出用。

DBF读写工具类

java 复制代码
package JRT.Core.Util;

import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFWriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供业务逻辑读写DBF文件的工具类。先创建空的DBF文件,再写入数据
 */
public class DbfUtil {

    /**
     * 创建dbf空文件
     *
     * @param path        文件路径
     * @param fields      字段信息
     * @param charsetName 编码字符集
     * @throws IOException
     */
    public static void CreateDbf(String path, DBFField[] fields, String charsetName) throws IOException {
        //定义DBFWriter实例用来写DBF文件
        DBFWriter dbfWriter = new DBFWriter(new FileOutputStream(path), Charset.forName(charsetName));
        //设置字段
        dbfWriter.setFields(fields);
        //写入dbf文件并关闭
        dbfWriter.close();
    }


    /**
     * 创建dbf空文件
     *
     * @param path        文件路径
     * @param fieldList   dbf字段,需要设定name,length两个参数,类型默认为字符串,可以参考从标准dbf文件中读取出来的样式。
     * @param charsetName 编码字符集                                                        编码字符集
     * @throws IOException
     */
    public static void CreateDbf(String path, List<Map<String, String>> fieldList, String charsetName) throws IOException {
        DBFField[] fields = new DBFField[fieldList.size()];
        int index = 0;
        for (Map<String, String> fieldMap : fieldList) {
            DBFField field = new DBFField();
            //字段名称
            field.setName(fieldMap.get("name"));
            //指定字段类型为字符串
            field.setType(DBFDataType.CHARACTER);
            //指定长度
            field.setLength(Integer.valueOf(fieldMap.get("length")));
            fields[index] = field;
            index++;
        }
        //定义DBFWriter实例用来写DBF文件
        DBFWriter dbfWriter = new DBFWriter(new FileOutputStream(path), Charset.forName(charsetName));
        //设置字段
        dbfWriter.setFields(fields);
        //写入dbf文件并关闭
        dbfWriter.close();
    }

    /**
     * 获取字段名
     *
     * @param path        路径
     * @param charsetName 字符集
     * @return 字段名数组
     * @throws IOException
     */
    public static String[] GetFieldName(String path, String charsetName) throws IOException {
        DBFReader dbfReader = new DBFReader(new FileInputStream(path), Charset.forName(charsetName));
        int fieldCount = dbfReader.getFieldCount();//获取字段数量
        String[] fieldName = new String[fieldCount];
        for (int i = 0; i < fieldCount; i++) {
            fieldName[i] = dbfReader.getField(i).getName();
        }
        dbfReader.close();
        return fieldName;
    }


    /**
     * 使用读取dbf文件作为模板,写dbf文件
     *
     * @param pathRead    dbf文件头模板
     * @param pathWriter  dbf写文件路径
     * @param rowList     要写入的记录行
     * @param charsetName 字符集
     * @throws IOException
     */
    public static void WriteDbf(String pathRead, String pathWriter, List<Map<String, Object>> rowList, String charsetName) throws IOException {
        DBFReader dbfReader = new DBFReader(new FileInputStream(pathRead), Charset.forName(charsetName));
        //获取字段数量
        int fieldCount = dbfReader.getFieldCount();
        DBFField[] fields = new DBFField[fieldCount];
        for (int i = 0; i < fieldCount; i++) {

            fields[i] = dbfReader.getField(i);
        }
        File fileWriter = new File(pathWriter);
        DBFWriter dbfWriter = new DBFWriter(fileWriter, Charset.forName(charsetName));
        //如果文件不存在,需要设置dbf文件字段头
        if (!fileWriter.exists()) {
            dbfWriter.setFields(fields);
        }
        //获取字段
        String[] fieldName = GetFieldName(pathRead, charsetName);
        for (Map<String, Object> rowMap : rowList) {
            Object[] rowData = new Object[fieldName.length];
            for (int i = 0; i < rowData.length; i++) {
                //根据字段来排列指,不然可能出现错位情况
                rowData[i] = rowMap.get(fieldName[i]);
            }
            //添加记录(此时并没有写入文件)
            dbfWriter.addRecord(rowData);
        }
        //写入dbf文件并保存关闭
        dbfWriter.close();
    }


    /**
     * 读dbf记录
     *
     * @param path 路径
     * @return 列表
     * @throws IOException
     */
    public static List<Map<String, Object>> ReadDbf(String path, String charsetName) throws IOException {
        List<Map<String, Object>> rowList = new ArrayList<Map<String, Object>>();
        DBFReader dbfReader = new DBFReader(new FileInputStream(path), Charset.forName(charsetName));
        Object[] rowValues;
        while ((rowValues = dbfReader.nextRecord()) != null) {
            Map<String, Object> rowMap = new HashMap<String, Object>();
            for (int i = 0; i < rowValues.length; i++) {
                rowMap.put(dbfReader.getField(i).getName(), rowValues[i]);
            }
            rowList.add(rowMap);
        }
        dbfReader.close();
        return rowList;
    }

}

测试工具类

java 复制代码
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试写DBF文件
 */
public class Main {
    /**
     * 测试摄像头操作
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        //构造列信息
        List<Map<String, String>> fieldList = new ArrayList<Map<String, String>>();
        Map<String, String> col1 = new HashMap<String, String>();
        col1.put("name", "Name");
        col1.put("length", "20");
        fieldList.add(col1);
        Map<String, String> col2 = new HashMap<String, String>();
        col2.put("name", "Age");
        col2.put("length", "20");
        fieldList.add(col2);
        Map<String, String> col3 = new HashMap<String, String>();
        col3.put("name", "Sex");
        col3.put("length", "20");
        fieldList.add(col3);
        Map<String, String> col4 = new HashMap<String, String>();
        col4.put("name", "Phone");
        col4.put("length", "20");
        fieldList.add(col4);
        Map<String, String> col5 = new HashMap<String, String>();
        col5.put("name", "Addr");
        col5.put("length", "20");
        fieldList.add(col5);
        //存在就删除
        File fi=new File("out.dbf");
        if(fi.exists())
        {
            fi.delete();
        }

        //创建空的dbf文件
        JRT.Core.Util.DbfUtil.CreateDbf("out.dbf", fieldList, "GBK");

        //构造数据行
        List<Map<String, Object>> rowList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 100; i++) {
            Map<String, Object> row = new HashMap<String, Object>();
            row.put("Name", "姓名" + i);
            row.put("Age", i + "");
            row.put("Sex", "");
            row.put("Phone", "123456");
            row.put("Addr", "湖南长沙");
            rowList.add(row);
        }
        //写入数据到dbf文件
        JRT.Core.Util.DbfUtil.WriteDbf("out.dbf", "out.dbf", rowList, "GBK");
    }
}

生成DBF文件

这样实现WhoNet导出基本没问题了

相关推荐
测试19985 小时前
软件测试 - 单元测试总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
Mahir087 小时前
Spring 循环依赖深度解密:从问题本质到三级缓存源码级解析
java·后端·spring·缓存·面试·循环依赖·三级缓存
曲幽7 小时前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
RyFit9 小时前
SpringAI 常见问题及解决方案大全
java·ai
石山代码9 小时前
C++ 内存分区 堆区
java·开发语言·c++
前端若水9 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
绝知此事9 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
无风听海9 小时前
C# 隐式转换深度解析
java·开发语言·c#
涛声依旧-底层原理研究所10 小时前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
一只大袋鼠10 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git