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. C++内存分配方式——小结

    1 内存分配方式 内存分配方式有如下三种: 从静态存储区域分配.内存在程序编译的时候就分配好了,这些内存在整个程序运行期间都存在,如全局变量.static变量等等. 在堆栈上分配.在函数执行期间,函数 ...

  2. 关于Android Studio更新后一直Refreshing的解决办法!

    今天更新了一下studio一直是这个问题 查了很多资料终于解决了 造成这个问题的原因是要更新的gradle版本和studio安装路径中的gradle版本不一致导致的 把他们改成一致的即可 在这个目录里 ...

  3. ZBrush中关于标记的特殊情况

    在ZBrush®中使用Marker标记调控板来记忆物体属性,因此能在任何时间回到标记并使用它给其他物体或改变物体作为参考点,在使用Marker标记调控板时回出现很多特殊情况,本文小编就这些特殊情况做一 ...

  4. Error running Tomcat 6: Address localhost:8080 is already in use

    错误原因:8080端口被其他的应用占用!解决方案:第一步,命令提示符号,执行命令:netstat –anoActive ConnectionsProto Local Address          ...

  5. Android 7.0 Gallery图库源码分析4 - SlotView手势监听及页面跳转

    上篇文章讲了初始化View时会实例化一个SlotView并监听其事件,至于它是怎么实现的,用的是Android自带的GestureDetector. GestureDetector是Android自带 ...

  6. vue自定义一个过滤器

    vue如何自定义一个过滤器 html代码: <div id="app"> <input type="text" v-model="m ...

  7. OO第三单元总结——JML规格设计

    • 1.JML语言的理论基础.应用工具链情况 JML(Java Modeling Language)—— java建模语言,是一种行为接口规范语言( behavioral interface spec ...

  8. CSS3特效——六面体

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. jenkins 安装遇到的坑

                        最后启用安全的时候遇到一点坑,直接写了一个用户上去,没有勾选权限,然后在登录就说没有 overright/等,然后需要修改配置文件conf.xml 在主目录下. ...

  10. SQL SERVER-约束

    NOT NULL - 指示某列不能存储 NULL 值. UNIQUE - 保证某列的每行必须有唯一的值. PRIMARY KEY - NOT NULL 和 UNIQUE 的结合.确保某列(或两个列多个 ...