java dbf_用javaDBF操作(读、写)DBF文件

/*** Copyright (C) 2009 numenzq studio. All Rights Reserved.*/

packagecom.linkstec.mot.job.util;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.lang.reflect.InvocationTargetException;importjava.math.BigDecimal;importjava.util.ArrayList;importjava.util.List;importorg.apache.commons.beanutils.PropertyUtils;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;importcom.linuxense.javadbf.DBFField;importcom.linuxense.javadbf.DBFReader;importcom.linuxense.javadbf.DBFWriter;/*** DBF 文件解析

**/

public classDbfUtil {private final static String CHARSET = "GB2312";/*** 读DBF文件

*@paramfile

*@paramclazz

*@paramtemplate

*@return*@throwsDocumentException

*@throwsInstantiationException

*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException

*@throwsIOException*/

public static List readDbf(File file, Classclazz, InputStream template)throwsDocumentException,

InstantiationException, IllegalAccessException, InvocationTargetException,

NoSuchMethodException, IOException {

FileInputStream fis= newFileInputStream(file);

DBFReader reader= newDBFReader(fis);

reader.setCharactersetName(CHARSET);

List propertys =readTemplate(template);for(Element element : propertys){int fieldsCount =reader.getFieldCount();for (int i = 0; i < fieldsCount; i++) {

DBFField field=reader.getField(i);if(field.getName().equals(element.attributeValue("column"))){

element.addAttribute("index", i+"");break;

}

}

}

List records = new ArrayList();for (int i = 0; i < reader.getRecordCount(); i++) {//System.out.println(i+1 + "/" + reader.getRecordCount());

records.add(readLine(clazz, propertys, reader.nextRecord()));

}

fis.close();returnrecords;

}/*** 写DBF文件

*@parambeans

*@paramtemplate

*@return*@throwsDocumentException

*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException

*@throwsIOException*/

public static byte[] writeDbf(List extends Object> beans, InputStream template) throwsDocumentException,

IllegalAccessException, InvocationTargetException, NoSuchMethodException,

IOException {

List propertys =readTemplate(template);

DBFWriter writer= newDBFWriter();

writer.setCharactersetName(CHARSET);

writer.setFields(writeFields(propertys));for (int i = 0; i < beans.size(); i++) {

writer.addRecord(writeLine(beans.get(i), propertys));

}

ByteArrayOutputStream baos= newByteArrayOutputStream();

writer.write(baos);returnbaos.toByteArray();

}/*** 写DBF文件(方式2)

*@parambeans

*@paramtemplate

*@paramfile

*@throwsDocumentException

*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException

*@throwsIOException*/

public static void writeDbf(List extends Object> beans, InputStream template,File file) throwsDocumentException,

IllegalAccessException, InvocationTargetException, NoSuchMethodException,

IOException {

List propertys =readTemplate(template);

DBFWriter writer= newDBFWriter(file);

writer.setCharactersetName(CHARSET);

writer.setFields(writeFields(propertys));for (int i = 0; i < beans.size(); i++) {

writer.addRecord(writeLine(beans.get(i), propertys));

}

writer.write();

}/*** SAX解析表结构模板

*@paramin

*@return*@throwsDocumentException*/@SuppressWarnings("unchecked")private static List readTemplate(InputStream in) throwsDocumentException {

SAXReader reader= newSAXReader();

Document document=reader.read(in);return (List)document.getRootElement().elements();

}/*** 读取一行数据

*@paramclazz

*@parampropertys

*@paramvalues

*@return*@throwsInstantiationException

*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

private static T readLine(Class clazz, Listpropertys, Object[] values)throwsInstantiationException, IllegalAccessException, InvocationTargetException,

NoSuchMethodException {

T bean=clazz.newInstance();for (int i = 0; i < propertys.size(); i++) {

Element property=propertys.get(i);if(property.attributeValue("index") != null && property.attributeValue("index").length() > 0){int index = Integer.parseInt(property.attributeValue("index"));

Object value=values[index];if (property.attributeValue("type").equals("C")) {

value=((String) value).trim();

}else if (property.attributeValue("type").equals("N")) {//if (property.attributeValue("scale")!=null && !"".equals(property.attributeValue("scale"))) {

value = newBigDecimal(((Double) value).doubleValue());//}else{//value = ((Double) value).longValue();//}

}//BeanUtil.copyProperty(bean, property.attributeValue("name"), value);

PropertyUtils.setProperty(bean, property.attributeValue("name"), value);

}

}returnbean;

}/*** 写一行数据

*@parambean

*@parampropertys

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

private static Object[] writeLine(Object bean, Listpropertys)throwsIllegalAccessException, InvocationTargetException, NoSuchMethodException {

Object[] row= newObject[propertys.size()];for (int i = 0; i < propertys.size(); i++) {

Element element=propertys.get(i);

row[i]= PropertyUtils.getProperty(bean, element.attributeValue("name"));

}returnrow;

}/*** 设置表结构

*@parampropertys

*@return

*/

private static DBFField[] writeFields(Listpropertys) {

DBFField[] fields= newDBFField[propertys.size()];for (int i = 0; i < propertys.size(); i++) {

Element property=propertys.get(i);

fields[i]= newDBFField();

fields[i].setName(property.attributeValue("column"));

fields[i].setDataType((byte) property.attributeValue("type").charAt(0));if (property.attributeValue("length") != null) {

fields[i].setFieldLength(Integer.parseInt(property.attributeValue("length")));

}if (property.attributeValue("scale") != null) {

fields[i].setDecimalCount(Integer.parseInt(property.attributeValue("scale")));

}

}returnfields;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值