<-- 依赖 dbf->
<dependency> <groupId>com.github.albfernandez</groupId> <artifactId>javadbf</artifactId> <version>1.13.2</version> </dependency>
package com.thtf.zwdsj.fangjia.utils; import com.linuxense.javadbf.DBFDataType; import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFReader; import com.linuxense.javadbf.DBFWriter; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.charset.Charset; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** * @创建人 qinyangyang * @创建时间 2023/02/27 * @描述 */ public class DbfWriterAndReadUtil { /** * 创建dbf空文件, * @param path:文件路径 * @param fieldList:dbf字段,需要设定name,type,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 { // InputStream fis = new FileInputStream(path); 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(); // fis.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)); //需要先设置好fileds,本方法fields与读取的dbf文件相同,所以直接从读取dbf文件拿。 //如果文件不存在,需要设置dbf文件字段头 if(!fileWriter.exists()){ dbfWriter.setFields(fields); } //获取字段 String[] fieldName = getFieldName(pathRead, "GBK"); 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]); } // rowMap.values().toArray(rowData); //添加记录(此时并没有写入文件) 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<>(); // InputStream fis = new FileInputStream(path); 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++) { if(rowValues[i] != null && !rowValues[i].equals("")){ rowMap.put(dbfReader.getField(i).getName(),rowValues[i]); } } if(!rowMap.isEmpty()){ rowList.add(rowMap); } } dbfReader.close(); // fis.close(); return rowList; } /** * 读dbf记录 * @param file * @return * @throws IOException */ public static List<Map<String, Object>> readDbf(MultipartFile file,String charsetName) throws IOException { List<Map<String, Object>> rowList = new ArrayList<>(); // InputStream fis = new FileInputStream(path); DBFReader dbfReader = new DBFReader(file.getInputStream(), 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++) { if(rowValues[i] != null && !rowValues[i].equals("")){ rowMap.put(dbfReader.getField(i).getName(),rowValues[i]); } } if(!rowMap.isEmpty()){ rowList.add(rowMap); } } dbfReader.close(); // fis.close(); return rowList; } }
标签:java,String,import,dbf,new,操作,dbfReader,charsetName From: https://www.cnblogs.com/chenTo/p/17285611.html