- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.List;
- import com.linuxense.javadbf.DBFField;
- import com.linuxense.javadbf.DBFWriter;
- /**
- * 写入dbf
- * @author zyf
- *
- */
- public class DbfUtil {
- private static final String CHARSET = "GBK";
- /**
- * 写入文件
- * @param beans
- * @param propertys
- * @return
- * @throws DocumentException
- * @throws IllegalAccessException
- * @throws InvocationTargetException
- * @throws NoSuchMethodException
- * @throws IOException
- */
- public static Boolean writeDbf(List beans,String dbfName) {
- try {
- OutputStream fos = null;
- List propertys = writeLine(beans.get(0));
- DBFWriter writer =new DBFWriter();
- writer.setCharactersetName(CHARSET);
- DBFField[] dbffs=writeFields(beans.get(0));
- writer.setFields(dbffs);
- for (int i = 0; i < beans.size(); i++) {
- writer.addRecord(writeLine(beans.get(i),propertys));
- }
- fos = new FileOutputStream(dbfName);
- writer.write(fos);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 设置写入dbf文件字段类型
- * @param propertys
- * @return
- */
- private static DBFField[] writeFields(List propertys) {
- DBFField[] fields = new DBFField[propertys.size()];
- for (int i = 0; i < propertys.size(); i++) {
- char c='C';
- String name=((String)propertys.get(i)).toUpperCase();
- fields[i] = new DBFField();
- fields[i].setName(name);
- fields[i].setDataType((byte) c);
- fields[i].setFieldLength(100);
- }
- return fields;
- }
- /**
- * 设置写入dbf文件字段类型
- * @param propertys
- * @return
- */
- private static DBFField[] writeFields(Object obj) {
- java.lang.reflect.Field[]field=obj.getClass().getDeclaredFields();
- DBFField[] fields = new DBFField[field.length];
- int i=0;
- for(java.lang.reflect.Field tempF:field){
- String name=tempF.getName().toUpperCase();
- fields[i] = new DBFField();
- fields[i].setName(name);
- if(tempF.getType().equals(BigDecimal.class)){
- fields[i].setDataType(DBFField.FIELD_TYPE_N);
- fields[i].setFieldLength(22);
- fields[i].setDecimalCount(2);
- }else{
- fields[i].setFieldLength(100);
- fields[i].setDataType(DBFField.FIELD_TYPE_C);
- }
- i++;
- }
- return fields;
- }
- /**
- * 返回每行匹配配置xml的数?
- * @param bean
- * @param propertys
- * @return
- * @throws IllegalAccessException
- * @throws InvocationTargetException
- * @throws NoSuchMethodException
- */
- private static Object[] writeLine(Object bean, List propertys)
- throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- List list=new ArrayList();
- printObj(bean,list);
- return list.toArray();
- }
- public static void printObj(Object obj, List list){
- Method[] meth=obj.getClass().getMethods();
- int i=0;
- for(Method tempM:meth){
- if(tempM.getName().indexOf("get")>-1&&tempM.getName().indexOf("getClass")==-1){
- try {
- Object objVaule=tempM.invoke(obj);
- //objVaule=objVaule==null?new String(""):objVaule;
- if(tempM.getReturnType().equals(BigDecimal.class)) {
- BigDecimal bd = new BigDecimal(objVaule.toString());
- list.add(bd.doubleValue());
- } else {
- list.add(objVaule);
- }
- i++;
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 返回每行匹配配置xml的数�?
- * @param bean
- * @param propertys
- * @return
- * @throws IllegalAccessException
- * @throws InvocationTargetException
- * @throws NoSuchMethodException
- */
- public static List writeLine(Object obj){
- List list=new ArrayList();
- java.lang.reflect.Field[]field=obj.getClass().getDeclaredFields();
- for(java.lang.reflect.Field tempF:field){
- list.add(tempF.getName());
- }
- return list;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1706201614733.html
来源: http://www.codesnippet.cn/detail/1706201614733.html