java写DBF文件

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

DBF读写工具类

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;
    }

}

测试工具类

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

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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库来解析整数和日期值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小乌鱼

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值