请使用下面依赖,尤其是 dbf文件 这个文件的依赖,不要升版本或者降低版本,升级版本了写出会有255个字段的限制
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.7</version>
</dependency>
<!-- dbf文件-->
<dependency>
<groupId>com.github.albfernandez</groupId>
<artifactId>javadbf</artifactId>
<version>1.9.4</version>
</dependency>
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFWriter;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author yren
* @create 2020-07-30
*/
public class DBFUtil{
public static void main(String[] args) throws Exception {
String fileName = "test1.dbf";
String newFileName = "test2.dbf";
List<Map<String, Object>> data = readData(fileName);
writeData(data, newFileName);
}
/**
* 读取 dbf 文件
*/
private static List<Map<String, Object>> readData(String fileName) throws Exception {
System.out.println("读取数据文件【" + fileName + "】开始...");
InputStream inputStream = new FileInputStream(fileName);
DBFReader reader = new DBFReader(inputStream, CharsetUtil.CHARSET_GBK);
List<Map<String, Object>> data = new ArrayList<>();
List<String> heads = new ArrayList<>();
int fieldsCount = reader.getFieldCount();
for (int i = 0; i < fieldsCount; i++) {
DBFField field = reader.getField(i);
heads.add(field.getName());
}
Object[] rowObjects;
while ((rowObjects = reader.nextRecord()) != null) {
Map<String, Object> line = new HashMap<>(400);
for (int i = 0; i < rowObjects.length; i++) {
line.put(heads.get(i), rowObjects[i]);
}
data.add(line);
}
inputStream.close();
System.out.println("读取数据完成...");
return data;
}
/**
* 写入 dbf 文件
*/
private static void writeData(List<Map<String, Object>> data, String newFileName) throws Exception {
if (CollUtil.isEmpty(data)) {
return;
}
List<String> head = new ArrayList<>(data.get(0).keySet());
final int size = head.size();
DBFField fields[] = new DBFField[size];
for (int i = 0; i < size; i++) {
fields[i] = new DBFField();
String fieldName = head.get(i);
fields[i].setName(fieldName);
// 设置成数字
// fields[i].setType(DBFDataType.NUMERIC);
// fields[i].setLength(20);
// fields[i].setDecimalCount(2);
//设置成字符串
fields[i].setType(DBFDataType.CHARACTER);
fields[i].setLength(50);
}
FileOutputStream fos = new FileOutputStream(newFileName);
DBFWriter writer = new DBFWriter(fos, CharsetUtil.CHARSET_GBK);
writer.setFields(fields);
for (Map<String, Object> line : data) {
Object[] rowData = new Object[size];
for (int i = 0; i < size; i++) {
Object fieldValue = line.get(fields[i].getName());
String value = fieldValue == null ? "" : fieldValue.toString();
if (fields[i].getType() == DBFDataType.NUMERIC) {
if (StrUtil.isBlank(value)) {
rowData[i] = Double.valueOf(0);
} else {
rowData[i] = Double.valueOf(value);
}
} else {
rowData[i] = value;
}
}
writer.addRecord(rowData);
}
writer.close();
fos.close();
System.out.println(newFileName + "写出数据完成...");
}
}
|