package cn.com.szhtkj.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.naming.NamingException; 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.DBFWriter; import cn.com.szhtkj.dto.SjxmDtoOutput; public class ExportDbf {
private static final String CHARSET = "GBK";
// private static File dataFile;
private final static int records = 2000;
/**
* 写入文件
* @param beans
* @param propertys
* @return
* @throws DocumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws IOException
*/
public static String writeDbf(List<?> beans, String templateName,String dbfName) throws DocumentException,
IllegalAccessException, InvocationTargetException, NoSuchMethodException,
IOException {
try {
List<?> propertys = readTemplate(templateName);
DBFWriter writer = new DBFWriter(new File(dbfName));
writer.setCharactersetName(CHARSET);
writer.setFields(writeFields(propertys));
for (int i = 0; i < beans.size(); i++) {
writer.addRecord(writeLine(beans.get(i), propertys));
}
writer.write();
return "succ";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
/**
* 写入文件
* @param beans
* @param propertys
* @return 第一次写入dbf已经写入dbf表结构,所以不需要二次写入
* @throws DocumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
* @throws IOException
*/
public static String writeDbf2(List<?> beans, String templateName,String dbfName) throws DocumentException,
IllegalAccessException, InvocationTargetException, NoSuchMethodException,
IOException {
try {
List<?> propertys = readTemplate(templateName);
DBFWriter writer = new DBFWriter(new File(dbfName));
writer.setCharactersetName(CHARSET);
writer.setFields(writeFields(propertys));
for (int i = 0; i < beans.size(); i++) {
writer.addRecord(writeLine(beans.get(i), propertys));
}
writer.write();
return "succ";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
/**
* 读取配置文件
* @param filename
* @return
* @throws DocumentException
*/
private static List<?> readTemplate(String filename) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(filename);
return document.getRootElement().elements();
}
/**
* 解析dbf文件
* @param clazz
* @param propertys
* @param values
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
@SuppressWarnings("unused")
private static Object readLine(Class<?> clazz, List<?> propertys, Object[] values)
throws InstantiationException, IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object bean = clazz.newInstance();
for (int i = 0; i < propertys.size(); i++) {
Element property = (Element) propertys.get(i);
Object value = values[i];
if (property.attributeValue("type").equals("C")) {
value = ((String) value).trim();
}
PropertyUtils.setProperty(bean, property.attributeValue("name"), value);
}
return bean;
}
/**
* 返回每行匹配配置xml的数�?
* @param bean
* @param propertys
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
private static Object[] writeLine(Object bean, List<?> propertys)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object[] row = new Object[propertys.size()];
for (int i = 0; i < propertys.size(); i++) {
Element element = (Element) propertys.get(i);
row[i] = PropertyUtils.getProperty(bean, element.attributeValue("name"));
}
return row;
}
/**
* 设置写入dbf文件字段类型
* @param propertys
* @return
*/
private static DBFField[] writeFields(List<?> propertys) {
DBFField[] fields = new DBFField[propertys.size()];
for (int i = 0; i < propertys.size(); i++) {
Element property = (Element) propertys.get(i);
fields[i] = new DBFField();
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")));
}
}
return fields;
}
/**
* 启动程序方法
* @throws NamingException
* @throws SQLException
* @throws IOException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws DocumentException
*/
@SuppressWarnings("static-access")
public static int setUp(String fcode, String xmlNameAndPath, List<SjxmDtoOutput> list) throws SQLException, NamingException, DocumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
File file = new File(fcode);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if(file.exists()){
deleteFile(fcode);
}
String succ="";
ExportDbf ep=new ExportDbf();
//取得总记录数,进行分页处理
long count = list.size();
if(count >0){
long page = count / records;
if (count % records != 0 ) {
page = page + 1 ;
}
//开始行号,结束行号
long start = 1 , end = records;
//根据页数进行循环
List<SjxmDtoOutput> orders = new ArrayList<SjxmDtoOutput>();
for ( long j = 1 ; j <= page; j++) {
if(list.size() > 0){
for(int i=0;i<list.size();i++){
SjxmDtoOutput order = new SjxmDtoOutput();
order.setBmbh(list.get(i).getBmbh());
order.setBmmc(list.get(i).getBmmc()+" ");
order.setSjxmdm(list.get(i).getSjxmdm());
order.setXmbh(list.get(i).getXmbh());
order.setXmmc(list.get(i).getXmmc()+" ");
orders.add(order);
}
//调用生成dbf方法
if(j >1){
succ=ep.writeDbf2(orders, xmlNameAndPath,fcode);
orders.clear();
}else{
succ=ep.writeDbf(orders, xmlNameAndPath,fcode);
orders.clear();
}
}
start = end + 1 ;
end = end + records;
}
}
if(count == 0){
return 99;
}else if(succ.equals("succ")){
return 88;
}else{
return 66;
} } /**
*
* 删除
* @param sPath
* @return
*/
public static boolean deleteFile(String sPath) {
Boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
public static void main(String[] args) throws Exception {
// ExportDbf dbf=new ExportDbf();
// File xing=new File(System.getProperty("user.dir"));
// String x=xing.getPath().replace("\\", "\\\\");
// String cs1=x+"\\export.dbf";
// System.out.println(cs1);
// setUp(cs1);
} }

Dbf文件操作的更多相关文章

  1. JAVA 操作 DBF 文件数据库

    1.依赖夹包 javadbf-[].4.1.jar jconn3.jar 2.添加属性文件 jdbc.properties jdbc.driverClassName=com.sybase.jdbc3. ...

  2. oracle_一次移动数据库dbf文件的操作

    oracle数据库的dbf路径下面磁盘不足,需要把原始路径下面的dbf文件移动到另外一个磁盘路径下, 具体的操作有四步. 1.把整个表空间offline. 2.copy原始路径下的dbf文件到新的路径 ...

  3. Java读取Level-1行情dbf文件极致优化(3)

    最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...

  4. C#生成DBF文件

    C# 生成DBF,无需注册Microsoft.Jet.OLEDB. namespace ConsoleApplication { class Program { static void Main(st ...

  5. JavaDBF:保存行情实时数据到DBF文件

    JavaDBF.jar其实很早都不再更新了,在日新月异的科技圈算得上远古上神的jar包,早该身归混沌了. 但我们的项目要用到,因为之前做的大宗期货交易行情的分析文件依然是dbf文件,没有办法,还得用 ...

  6. 由于删除DBF文件报错 —— ORA-01033: ORACLE initialization or shutdown in progress

    由于移动或删除DBF文件报错:ORA-01033: ORACLE initialization or shutdown in progress   原因:一般该类故障通常是由于移动文件而影响了数据库日 ...

  7. 《Java知识应用》Java读写DBF文件

    1. 准备: Jar包下载:链接: https://pan.baidu.com/s/1Ikxx-vkw5vSDf9SBUQHBCw 提取码: 7h58 复制这段内容后打开百度网盘手机App,操作更方便 ...

  8. DBF 文件 ORACLE 数据库恢复

    DBF 文件 ORACLE 数据库恢复 清·魏源<庸易通义>:"至道问学之有知无行,分温故为存心,知新为致知,而敦厚为存心,崇礼为致知,此皆百密一疏." 起因 在我们的 ...

  9. Oracle的dbf文件迁移

    1.背景说明 在Oracle数据库中插入了1.5亿条数据, 并且创建了主键索引, 又插入了1.5亿条数据到另外一张表, 导致数据库表空间暴涨到28G, 由于根目录下只有50G的空间, 数据库文件所在磁 ...

随机推荐

  1. Hibernate框架学习(五)——批量查询(概述)

    一.HQL查询(Hibernate Query Language)多表查询,但不复杂时使用 Hibernate独家查询语言,属于面向对象的查询语言 1.基本查询 2.条件查询 注意:HQL语句中不可能 ...

  2. tomcat 和 jboss的热部署(热发布)问题

    所谓的热部署(热发布)(下面称为“热部署”),就是说,在web工程发布之后,不可避免的,会遇到修改BUG的问题.现在的热部署就是为了解决这个问题,其功能就是说:在不停止web服务的同时,对jsp和Ja ...

  3. 【摘录】JAVA内存管理-评估垃圾收集性能的工具

    第七章 评估垃圾收集性能的工具 各种各样的诊断和监视工具可以用来评估垃圾收集性能.本章简要概述他们中的几个.可以通过第九章中的“Tools and Troubleshooting”链接获得更多的信息. ...

  4. spring的JdbcTemplate

    一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象.       第一种方式:我们可以在自己定 ...

  5. react-redux源码解析(资料)

    资料:https://www.cnblogs.com/hhhyaaon/p/5863408.html 感觉很棒,记录一下

  6. Git diff 代码比较的高级技巧

    Git diff 代码比较的高级技巧 作者:offbye 出处:http://blog.csdn.net/offbye/article/details/6592563 Git是使用branch来管理不 ...

  7. Android 开发者必知的开发资源

    英文原文:Bongzimo  翻译: ImportNew-黄小非 译文链接:http://www.importnew.com/3988.html Android 开发者必知的开发资源 随着Androi ...

  8. HTML 捕获window.close() 并做窗口关闭前的处理工作

    转自:http://www.xinotes.net/notes/note/261/ <html> <head> <script language="JavaSc ...

  9. [LUOGU]2016 Sam数

    我本来想看看SAM,就看见了这个.. 这道题很容易让人想到数位DP,用\(f[i][j]\)表示考虑到第\(i\)位,最后一位是\(j\)的方案数.看到1e18,直接矩阵快速幂加速,因为它每位转移都是 ...

  10. [读书笔记] R语言实战 (十四) 主成分和因子分析

    主成分分析和探索性因子分析是用来探索和简化多变量复杂关系的常用方法,能解决信息过度复杂的多变量数据问题. 主成分分析PCA:一种数据降维技巧,将大量相关变量转化为一组很少的不相关变量,这些无关变量称为 ...