当前位置:网站首页 > Java基础 > 正文

java dbf教程



/

  • 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&lt;Element&gt; 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 &lt; 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&lt;T&gt; records = new ArrayList&lt;T&gt;(); for (int i = 0; i &lt; 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&lt;? extends Object&gt; beans, InputStream template) throws DocumentException,
      IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException { 

      List&lt;Element&gt; propertys = readTemplate(template);

      DBFWriter writer = new DBFWriter(); writer.setCharactersetName(CHARSET); writer.setFields(writeFields(propertys));

      for (int i = 0; i &lt; 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&lt;? extends Object&gt; beans, InputStream template,File file) throws DocumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException { List&lt;Element&gt; propertys = readTemplate(template); DBFWriter writer = new DBFWriter(file); writer.setCharactersetName(CHARSET); writer.setFields(writeFields(propertys));

      for (int i = 0; i &lt; beans.size(); i++) {

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

      } writer.write(); }

      /

    • SAX解析表结构模板
    • @param in
    • @return
    • @throws DocumentException */ @SuppressWarnings(“unchecked”) private static List&lt;Element&gt; readTemplate(InputStream in) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(in); return (List&lt;Element&gt;)document.getRootElement().elements(); }

      /

    • 读取一行数据
    • @param clazz
    • @param propertys
    • @param values
    • @return
    • @throws InstantiationException
    • @throws IllegalAccessException
    • @throws InvocationTargetException
    • @throws NoSuchMethodException */ private static &lt;T&gt; T readLine(Class&lt;T&gt; clazz, List&lt;Element&gt; 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 &lt; 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> &amp;&amp; property.attributeValue("index").length() &gt; 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&lt;Element&gt; 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 &lt; 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&lt;Element&gt; propertys) { DBFField[] fields = new DBFField[propertys.size()]; for (int i = 0; i &lt; 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; }

}

  • 上一篇: java六代教程
  • 下一篇: java游戏编写教程
  • 版权声明


    相关文章:

  • java六代教程2026-01-23 10:58:05
  • java版联机教程2026-01-23 10:58:05
  • java教程 微盘2026-01-23 10:58:05
  • java tld 教程2026-01-23 10:58:05
  • java算法教程 20172026-01-23 10:58:05
  • java游戏编写教程2026-01-23 10:58:05
  • java文字教程2026-01-23 10:58:05
  • 经验塔java版教程2026-01-23 10:58:05
  • 阿里Java教程2026-01-23 10:58:05
  • java编程艺术教程2026-01-23 10:58:05