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导出基本没问题了

相关推荐
kszlgy1 小时前
Day 52 神经网络调参指南
python
Leo July3 小时前
【Java】Spring Security 6.x 全解析:从基础认证到企业级权限架构
java·spring·架构
wrj的博客3 小时前
python环境安装
python·学习·环境配置
Pyeako3 小时前
深度学习--BP神经网络&梯度下降&损失函数
人工智能·python·深度学习·bp神经网络·损失函数·梯度下降·正则化惩罚
星火开发设计3 小时前
C++ 数组:一维数组的定义、遍历与常见操作
java·开发语言·数据结构·c++·学习·数组·知识
码道功成3 小时前
Pycham及IntelliJ Idea常用插件
java·ide·intellij-idea
消失的旧时光-19434 小时前
第四篇(实战): 订单表索引设计实战:从慢 SQL 到毫秒级
java·数据库·sql
それども4 小时前
@ModelAttribute vs @RequestBody
java
摘星编程4 小时前
OpenHarmony环境下React Native:Geolocation地理围栏
python
充值修改昵称5 小时前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法