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

相关推荐
程序员是干活的几秒前
私家车开车回家过节会发生什么事情
java·开发语言·软件构建·1024程序员节
煸橙干儿~~11 分钟前
分析JS Crash(进程崩溃)
java·前端·javascript
2401_8543910812 分钟前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
Envyᥫᩣ12 分钟前
Python中的自然语言处理:从基础到高级
python·自然语言处理·easyui
Amor风信子13 分钟前
华为OD机试真题---跳房子II
java·数据结构·算法
哪 吒13 分钟前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
我是陈泽16 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
hakesashou16 分钟前
python全栈开发是什么?
python
创作小达人35 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
杨荧39 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源