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. 使用Latex写book类型文本的体会

    晚上参考中科院上海交大清华北大等学校的模板,终于重新把博一时候没解决的问题解决了.中科院吴老师的CTeX论坛因为维护压力比较大不得不关了,查不到之前的Latex解决答案.经过一下午和一晚上的摸索,忽然 ...

  2. .net 三大核心对象

    .net 三大核心对象 HttpRequest 现在总算轮到第一个核心对象出场了.MSDN给它作了一个简短的解释:“使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值.”这个解 ...

  3. 发现个很变态的css问题,记录下。

    注意看Ul:nth-child(){} 本来是一一对应的,因为后来改版在div中加了<h1></h1>标记后,竟然自动后移了.... 不可思议,莫明奇妙

  4. JS 日历

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  5. Layui Excle/csv数据导出

    官方文档的数据是这样的 依赖 Layui 2.4版本以上 layui.use([ 'table'], function(){ var table=layui.table; table.exportFi ...

  6. 集成spring boot + mysql + docker实战

    前言 网上找过很多文章,关于通过docker构建mysql容器并将应用容器和docker容器关联起来的文章不多.本文将给出具体的范例.此处为项目的源码 前置条件 该教程要求在宿主机上配置了: dock ...

  7. 题解 CF821D 【Okabe and City】

    其实,这道题不用long long也能AC. 题意是给你一个矩阵,有一些格子被点亮有一些没有,每一次只能在被点亮的格子上面走. 然后你每一次都可以选择点亮一行或一排(非永久),现在问你最少点多少次可以 ...

  8. 【codeforces 794A】Bank Robbery

    [题目链接]:http://codeforces.com/contest/794/problem/A [题意] 每个位置上可能有物品(>=1)或是没物品 你一开始在某一个位置b; 然后你最左可以 ...

  9. cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)

    援引:http://blog.csdn.net/zhanghefu/article/details/21284323 http://blog.csdn.net/cxf7394373/article/d ...

  10. Go语言核心之美 1.1-命名篇

    命名篇 1.Go的函数.变量.常量.自己定义类型.包(Package)的命名方式遵循以下规则: 1)首字符能够是随意的Unicode字符或者下划线 2)剩余字符能够是Unicode字符.下划线.数字 ...