/
- Copyright © 2009 numenzq studio. All Rights Reserved. */ package com.linkstec.mot.job.util;
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List;
import org.apache.commons.beanutils.PropertyUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader;
import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFReader; import com.linuxense.javadbf.DBFWriter;
/
- DBF 文件解析 * */ public class DbfUtil {
private final static String CHARSET = “GB2312”; /
- 读DBF文件
- @param file
- @param clazz
- @param template
- @return
- @throws DocumentException
- @throws InstantiationException
- @throws IllegalAccessException
- @throws InvocationTargetException
- @throws NoSuchMethodException
- @throws IOException */ public static <T> List<T> readDbf(File file, Class<T> clazz, InputStream template)
</span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> DocumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {FileInputStream fis = new FileInputStream(file); DBFReader reader = new DBFReader(fis); reader.setCharactersetName(CHARSET);
List<Element> propertys = readTemplate(template); for(Element element : propertys){
</span><span style="color: rgba(0, 0, 255, 1)">int</span> fieldsCount =<span style="color: rgba(0, 0, 0, 1)"> reader.getFieldCount(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < fieldsCount; i++<span style="color: rgba(0, 0, 0, 1)">) { DBFField field </span>=<span style="color: rgba(0, 0, 0, 1)"> reader.getField(i); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(field.getName().equals(element.attributeValue("column"<span style="color: rgba(0, 0, 0, 1)">))){ element.addAttribute(</span>"index", i+""<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } }}
List<T> records = new ArrayList<T>(); for (int i = 0; i < reader.getRecordCount(); i++) { // System.out.println(i+1 + “/” + reader.getRecordCount()); records.add(readLine(clazz, propertys, reader.nextRecord())); }
fis.close(); return records; }
/
- 写DBF文件
- @param beans
- @param template
- @return
- @throws DocumentException
- @throws IllegalAccessException
- @throws InvocationTargetException
- @throws NoSuchMethodException
- @throws IOException */ public static byte[] writeDbf(List<? extends Object> beans, InputStream template) throws DocumentException,
IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {List<Element> propertys = readTemplate(template);
DBFWriter writer = new DBFWriter(); writer.setCharactersetName(CHARSET); writer.setFields(writeFields(propertys));
for (int i = 0; i < beans.size(); i++) {
writer.addRecord(writeLine(beans.get(i), propertys));}
ByteArrayOutputStream baos = new ByteArrayOutputStream(); writer.write(baos);
return baos.toByteArray(); }
/
- 写DBF文件(方式2)
- @param beans
- @param template
- @param file
- @throws DocumentException
- @throws IllegalAccessException
- @throws InvocationTargetException
- @throws NoSuchMethodException
- @throws IOException */ public static void writeDbf(List<? extends Object> beans, InputStream template,File file) throws DocumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException { List<Element> propertys = readTemplate(template); DBFWriter writer = new DBFWriter(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解析表结构模板
- @param in
- @return
- @throws DocumentException */ @SuppressWarnings(“unchecked”) private static List<Element> readTemplate(InputStream in) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(in); return (List<Element>)document.getRootElement().elements(); }
/
- 读取一行数据
- @param clazz
- @param propertys
- @param values
- @return
- @throws InstantiationException
- @throws IllegalAccessException
- @throws InvocationTargetException
- @throws NoSuchMethodException */ private static <T> T readLine(Class<T> clazz, List<Element> propertys, Object[] values)
</span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {T bean = clazz.newInstance(); for (int i = 0; i < propertys.size(); i++) {
Element property </span>=<span style="color: rgba(0, 0, 0, 1)"> propertys.get(i); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(property.attributeValue("index") != <span style="color: rgba(0, 0, 255, 1)">null</span> && property.attributeValue("index").length() > 0<span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">int</span> index = Integer.parseInt(property.attributeValue("index"<span style="color: rgba(0, 0, 0, 1)">)); Object value </span>=<span style="color: rgba(0, 0, 0, 1)"> values[index]; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (property.attributeValue("type").equals("C"<span style="color: rgba(0, 0, 0, 1)">)) { value </span>=<span style="color: rgba(0, 0, 0, 1)"> ((String) value).trim(); }</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (property.attributeValue("type").equals("N"<span style="color: rgba(0, 0, 0, 1)">)) {// if (property.attributeValue(“scale”)!=null && !“”.equals(property.attributeValue(“scale”))) {
value = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BigDecimal(((Double) value).doubleValue());// }else{ // value = ((Double) value).longValue(); // }
}// BeanUtil.copyProperty(bean, property.attributeValue(“name”), value);
PropertyUtils.setProperty(bean, property.attributeValue("name"<span style="color: rgba(0, 0, 0, 1)">), value); }} return bean; }
/
- 写一行数据
- @param bean
- @param propertys
- @return
- @throws IllegalAccessException
- @throws InvocationTargetException
- @throws NoSuchMethodException */ private static Object[] writeLine(Object bean, List<Element> propertys)
</span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IllegalAccessException, InvocationTargetException, NoSuchMethodException {Object[] row = new Object[propertys.size()]; for (int i = 0; i < propertys.size(); i++) {
Element element </span>=<span style="color: rgba(0, 0, 0, 1)"> propertys.get(i); row[i] </span>= PropertyUtils.getProperty(bean, element.attributeValue("name"<span style="color: rgba(0, 0, 0, 1)">));}
return row; }
/
- 设置表结构
- @param propertys
- @return */ private static DBFField[] writeFields(List<Element> propertys) { DBFField[] fields = new DBFField[propertys.size()]; for (int i = 0; i < propertys.size(); i++) {
Element property </span>=<span style="color: rgba(0, 0, 0, 1)"> propertys.get(i); fields[i] </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DBFField(); fields[i].setName(property.attributeValue(</span>"column"<span style="color: rgba(0, 0, 0, 1)">)); fields[i].setDataType((</span><span style="color: rgba(0, 0, 255, 1)">byte</span>) property.attributeValue("type").charAt(0<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (property.attributeValue("length") != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { fields[i].setFieldLength(Integer.parseInt(property.attributeValue(</span>"length"<span style="color: rgba(0, 0, 0, 1)">))); } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (property.attributeValue("scale") != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { fields[i].setDecimalCount(Integer.parseInt(property.attributeValue(</span>"scale"<span style="color: rgba(0, 0, 0, 1)">))); }} return fields; }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/8405.html