dbf文件解析
废话可直接用excel打开开搞导包<!--DBF--><dependency><groupId>com.github.albfernandez</groupId><artifactId>javadbf</artifactId><version>1.9.2</version>
·
废话
可直接用excel打开
开搞
导包
<!-- DBF-->
<dependency>
<groupId>com.github.albfernandez</groupId>
<artifactId>javadbf</artifactId>
<version>1.9.2</version>
</dependency>
工具类
public class DbfUtils {
/**
* 读取全部数据
*
* @param path
*/
public static List<Map<String, String>> readDbf(String path, String charsetName) throws IOException {
if (charsetName == null) {
charsetName = "GBK";
}
List<Map<String, String>> rowList = new ArrayList<>();
DBFReader dbfReader = new DBFReader(new FileInputStream(path), Charset.forName(charsetName));
Object[] rowValues;
while ((rowValues = dbfReader.nextRecord()) != null) {
Map<String, String> rowMap = new HashMap<>();
for (int i = 0; i < rowValues.length; i++) {
rowMap.put(dbfReader.getField(i).getName(), String.valueOf(rowValues[i]).trim());
}
rowList.add(rowMap);
}
dbfReader.close();
return rowList;
}
/**
* 获取dbf文件数据的字段名
*
* @param path 文件路径
* @param charsetName 字符集编码
*/
public static String[] getFieldName(String path, String charsetName) throws IOException {
if (charsetName == null) {
charsetName = "GBK";
}
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文件
*
* @param path 文件路径
* @param fieldList 字段
* @param charsetName 字符集编码
*/
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 rowList 需要写入的数据
* @param charsetName 字符集
*/
public static void writeDbf(String path, List<Map<String, String>> rowList, String charsetName)
throws IOException {
if (charsetName == null) {
charsetName = "GBK";
}
DBFWriter dbfWriter = new DBFWriter(new File(path));
//获取字段
String[] fieldName = getFieldName(path, charsetName);
for (Map<String, String> 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();
}
}
调用
public static void main(String[] args) {
try {
String filePath = "C:\\Users\\Admin\\Desktop\\test.dbf";
String[] title= DbfUtils.getFieldName(filePath,"GBK");
List<Map<String, String>> list= DbfUtils.readDbf(filePath,"GBK");
log.info("标题:{}", JSON.toJSONString(title));
log.info("解析的数据:{}", JSON.toJSONString(list));
} catch (Exception e) {
e.printStackTrace();
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)