package com.cplatform.util2.dbftool;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import com.cplatform.util2.dbftool.exception.ColumnTooLargeException;
import com.cplatform.util2.dbftool.lang.Column;
import com.cplatform.util2.dbftool.lang.Condition;
import com.cplatform.util2.dbftool.lang.DbfResultSet;
import com.cplatform.util2.dbftool.lang.Record;
import com.cplatform.util2.dbftool.lang.Relation;
/**
* DBF操作类(DBF4.0)
* DBF文件以"\r"作为分隔符
* @author liujun
*
*/
public class DbfTool
{
private Logger logger=null;
private String dbfName = null;
private DbfResultSet rs=null;
private Vector columns = null;
private int columnNumber = 0;
private int rowLength = 0;
private static String TMP_FILE = "runtime.dat";
/**
* 默认构造函数,初始化日志
*
*/
public DbfTool() throws IOException
{
//整合入程序时请修改下一行
DOMConfigurator.configure("config/dbflog.xml");
logger = Logger.getLogger(this.getClass());
initColumns();
}
/**
* 打开指定dbf文件
* @param dbfName 所打开的dbf文件全路径名
* @return
*/
public void openTable(String dbfName)
{
this.dbfName = dbfName;
}
/**
* 关闭所打开的文件
* @return
* @throws IOException
*/
public int closeTable() throws IOException
{
this.dbfName = null;
columns.clear();
columns = null;
if(this.rs!=null) rs.close();
this.rs = null;
return -1;
}
/**
* 返回表中所有记录
* 完成后应调用结果集的关闭方法,否则会发生IO异常
* @return
* @throws FileNotFoundException
*/
public DbfResultSet selectAll() throws FileNotFoundException
{
try
{
if(rs!=null) rs.close();
}
catch(Exception ex)
{
logger.warn(null, ex);
}
rs=new DbfResultSet(this.dbfName, this.columns, this.rowLength, null);
return rs;
}
/**
* 根据查询条件返回结果集
* 完成后应调用结果集的关闭方法,否则会发生IO异常
* @param conditions 条件数组
* @return
* @throws FileNotFoundException
*/
public DbfResultSet select(Condition[] conditions) throws FileNotFoundException
{
try
{
if(rs!=null) rs.close();
}
catch(Exception ex)
{
logger.warn(null, ex);
}
rs=new DbfResultSet(this.dbfName, this.columns, this.rowLength, conditions);
return rs;
}
/**
* 向文件内添加记录
* @param rows
* @return 返回添加的记录数
*/
public int add(Record record) throws ColumnTooLargeException
{
File file =null;
FileOutputStream fos = null;
int result = 0;
try
{
file = new File(this.dbfName);
fos = new FileOutputStream(file, true);
}
catch(Exception ex)
{
return -1;
}
if(record == null) return 0;
byte[] buff=buildRecord(record);
try
{
fos.write(buff);
fos.flush();
fos.close();
result++;
}
catch(Exception ex)
{
logger.warn(null, ex);
return -1;
}
return result;
}
/**
* 向文件内添加记录
* @param rows
* @return 返回添加的记录数
*/
public int add(Record[] rows) throws ColumnTooLargeException
{
File file =null;
FileOutputStream fos = null;
int result = 0;
try
{
file = new File(this.dbfName);
fos = new FileOutputStream(file, true);
}
catch(Exception ex)
{
return -1;
}
for(int i=0; i< rows.length; i++)
{
Record record = rows[i];
if(record == null) continue;
byte[] buff=buildRecord(record);
try
{
fos.write(buff);
result++;
}
catch(Exception ex)
{
logger.warn(null, ex);
continue;
}
}
try
{
fos.flush();
fos.close();
}
catch(IOException ex)
{
logger.error(null, ex);
return -1;
}
return result;
}
/**
* 从文件内删除指定条件记录
* @param conditions
* @return
*/
public int delete(Condition[] conditions)
{
File tmpFile = new File(TMP_FILE);
File targetFile = new File(this.dbfName);
FileOutputStream fos = null;
DbfResultSet drs = null;
int result = 0;
RandomAccessFile fis = null;
try
{
fos = new FileOutputStream(tmpFile);
drs = new DbfResultSet(this.dbfName, this.columns, this.rowLength, null);
fis = new RandomAccessFile(targetFile, "r");
byte[] buff = new byte[(columns.size()+1)*32 + 1];
fis.readFully(buff);
fis.close();
fis = null;
fos.write(buff);
while(drs.next())
{
boolean tmp = false;
for(int i=0; i<conditions.length; i++)
{
Condition condition = conditions[i];
if(condition == null) continue;
String value = drs.getString(condition.getColumnName());
int relation = condition.getRelation();
if(relation == Condition.EQUAL)
{
if(value.equals(condition.getColumnValue()))
{
result ++;
tmp = true;
continue;
}
else
{
tmp = false;
break;
}
}
else if(relation == Condition.LESSTHAN)
{
if(value.compareTo(condition.getColumnValue()) <0)
{
result ++;
tmp = true;
continue;
}
else
{
tmp = false;
break;
}
}
else if(relation == Condition.MORETHAN)
{
if(value.compareTo(condition.getColumnValue()) >0)
{
result ++;
tmp = true;
continue;
}
else
{
tmp = false;
break;
}
}
else
tmp = false;
}
if(!tmp)
{
fos.write(buildRecord(drs.getCurrRecord()));
}
}
fos.flush();
}
catch(IOException ex)
{
logger.error(null, ex);
return -1;
}
catch(ColumnTooLargeException ex)
{
logger.error(null, ex);
return -1;
}
finally
{
if(drs!=null)
{
try
{
drs.close();
}
catch(Exception ex)
{
logger.error(null, ex);
}
}
if(fos!=null)
{
try
{
fos.close();
fos = null;
}
catch(Exception ex)
{
logger.error(null, ex);
}
}
if(fis!=null)
{
try
{
fis.close();
fis = null;
}
catch(Exception ex)
{
logger.error(null, ex);
}
}
}
try
{
targetFile.delete();
tmpFile.renameTo(targetFile);
}
catch(Exception ex)
{
logger.error(null, ex);
return -1;
}
return result;
}
/**
* 更新指定条件的记录
* @param records
* @param conditions
* @return
*/
public int update(Relation[] relations, Condition[] conditions)
{
RandomAccessFile raf = null;
int result = 0;
try
{
raf = new RandomAccessFile(this.dbfName, "rw");
long cursor = (columns.size()+1)*32;
raf.seek(cursor);
byte bBlank = 0x00;
while (bBlank == 0x00)
{
cursor++;
bBlank = raf.readByte();
continue;
}
byte[] row = new byte[this.rowLength];
while(true)
{
raf.readFully(row);
Record record = parseRecord(row);
if(checkIsValid(record, conditions))
{
record.addRelations(relations);
try
{
raf.seek(cursor);
raf.write(buildRecord(record));
result++;
}
catch(Exception ex)
{
logger.error(null, ex);
}
}
cursor += this.rowLength;
}
}
catch(EOFException ex)
{
return result;
}
catch(Exception ex)
{
logger.error(null, ex);
return 0-result;
}
finally
{
if(raf !=
常用工具包_DBF解析_20080304.rar
需积分: 10 119 浏览量
2009-01-04
16:31:46
上传
评论
收藏 7KB RAR 举报
wudangpeng
- 粉丝: 0
- 资源: 1
最新资源
- 原生微信小程序源码 - -滴滴公交-查公交
- 人工智能实验四 感知器算法的设计实现
- java小项目多线程多线程 复制文件 冒泡排序 群聊
- 四数之和(java代码).docx
- 701837906919458TapScanner v3.0.10 (Pro).apk
- 青岛大学人工智能实验二 利用α-β搜索的博弈树算法编写一字棋游戏
- ### 1、项目介绍 本项目Scrapy进行数据爬取,并使用Django框架+PyEcharts实现可视化大屏 效果如下:
- # 微信小程序-健康菜谱 基于微信小程序的一个查找检索菜谱的应用 ### 效果 !动态图(./res/gif/demo
- zabbix-get命令包资源
- 289ssm-mysql-jsp 计算机课程实验管理系统.zip(可运行源码+数据库文件+文档)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0