- 浏览: 249822 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
no_studio:
期待实现SqlServer
发布异种数据库导入工具jmyetl-1.0.2 -
babydeed:
不错 再接再厉
发布异种数据库导入工具jmyetl-1.0.2 -
iihero:
niwtsew 写道贴个俺自己写的linux下的版本,其实没必 ...
命令行快速找出class文件所在的jar包 -
niwtsew:
说错,是strings不是string
命令行快速找出class文件所在的jar包 -
niwtsew:
贴个俺自己写的linux下的版本,其实没必要用7z,直接jar ...
命令行快速找出class文件所在的jar包
我将dbf文件的读写基本分成四个类,Writer,Reader, Field, Exception,内容如下,如果在使用该程序包时有什么问题或者好的建议,请发mail到 iiihero#hotmail.com
下载的过程详见:http://computer.mblogger.cn/ehero/posts/31204.aspx
如果您能多Re几下本文,我将更为高兴。
/**
* <p>Title: java访问DBF文件的接口</p>
* <p>Description: 这个类用于表示DBF文件中的写操作</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ict</p>
* @author : He Xiong
* @version 1.0
*/
package com.hexiong.jdbf;
import java.io.*;
import java.util.Calendar;
// Referenced classes of package com.hexiong.jdbf:
// JDBFException, JDBField
public class DBFWriter
{
public DBFWriter(String s, JDBField ajdbfield[])
throws JDBFException
{
stream = null;
recCount = 0;
fields = null;
fileName = null;
dbfEncoding = null;
fileName = s;
try
{
init(new FileOutputStream(s), ajdbfield);
}
catch(FileNotFoundException filenotfoundexception)
{
throw new JDBFException(filenotfoundexception);
}
}
public DBFWriter(OutputStream outputstream, JDBField ajdbfield[])
throws JDBFException
{
stream = null;
recCount = 0;
fields = null;
fileName = null;
dbfEncoding = null;
init(outputstream, ajdbfield);
}
public DBFWriter(String s, JDBField ajdbfield[], String s1)
throws JDBFException
{
stream = null;
recCount = 0;
fields = null;
fileName = null;
dbfEncoding = null;
fileName = s;
try
{
dbfEncoding = s1;
init(new FileOutputStream(s), ajdbfield);
}
catch(FileNotFoundException filenotfoundexception)
{
throw new JDBFException(filenotfoundexception);
}
}
private void init(OutputStream outputstream, JDBField ajdbfield[])
throws JDBFException
{
fields = ajdbfield;
try
{
stream = new BufferedOutputStream(outputstream);
writeHeader();
for(int i = 0; i < ajdbfield.length; i++)
writeFieldHeader(ajdbfield[i]);
stream.write(13);
stream.flush();
}
catch(Exception exception)
{
throw new JDBFException(exception);
}
}
private void writeHeader()
throws IOException
{
byte abyte0[] = new byte[16];
abyte0[0] = 3;
Calendar calendar = Calendar.getInstance();
abyte0[1] = (byte)(calendar.get(1) - 1900);
abyte0[2] = (byte)calendar.get(2);
abyte0[3] = (byte)calendar.get(5);
abyte0[4] = 0;
abyte0[5] = 0;
abyte0[6] = 0;
abyte0[7] = 0;
int i = (fields.length + 1) * 32 + 1;
abyte0[8] = (byte)(i % 256);
abyte0[9] = (byte)(i / 256);
int j = 1;
for(int k = 0; k < fields.length; k++)
j += fields[k].getLength();
abyte0[10] = (byte)(j % 256);
abyte0[11] = (byte)(j / 256);
abyte0[12] = 0;
abyte0[13] = 0;
abyte0[14] = 0;
abyte0[15] = 0;
stream.write(abyte0, 0, abyte0.length);
for(int l = 0; l < 16; l++)
abyte0[l] = 0;
stream.write(abyte0, 0, abyte0.length);
}
private void writeFieldHeader(JDBField jdbfield)
throws IOException
{
byte abyte0[] = new byte[16];
String s = jdbfield.getName();
int i = s.length();
if(i > 10)
i = 10;
for(int j = 0; j < i; j++)
abyte0[j] = (byte)s.charAt(j);
for(int k = i; k <= 10; k++)
abyte0[k] = 0;
abyte0[11] = (byte)jdbfield.getType();
abyte0[12] = 0;
abyte0[13] = 0;
abyte0[14] = 0;
abyte0[15] = 0;
stream.write(abyte0, 0, abyte0.length);
for(int l = 0; l < 16; l++)
abyte0[l] = 0;
abyte0[0] = (byte)jdbfield.getLength();
abyte0[1] = (byte)jdbfield.getDecimalCount();
stream.write(abyte0, 0, abyte0.length);
}
public void addRecord(Object aobj[])
throws JDBFException
{
if(aobj.length != fields.length)
throw new JDBFException("Error adding record: Wrong number of values. Expected " + fields.length + ", got " + aobj.length + ".");
int i = 0;
for(int j = 0; j < fields.length; j++)
i += fields[j].getLength();
byte abyte0[] = new byte[i];
int k = 0;
for(int l = 0; l < fields.length; l++)
{
String s = fields[l].format(aobj[l]);
byte abyte1[];
try
{
if(dbfEncoding != null)
abyte1 = s.getBytes(dbfEncoding);
else
abyte1 = s.getBytes();
}
catch(UnsupportedEncodingException unsupportedencodingexception)
{
throw new JDBFException(unsupportedencodingexception);
}
for(int i1 = 0; i1 < fields[l].getLength(); i1++)
abyte0[k + i1] = abyte1[i1];
k += fields[l].getLength();
}
try
{
stream.write(32);
stream.write(abyte0, 0, abyte0.length);
stream.flush();
}
catch(IOException ioexception)
{
throw new JDBFException(ioexception);
}
recCount++;
}
public void close()
throws JDBFException
{
try
{
stream.write(26);
stream.close();
RandomAccessFile randomaccessfile = new RandomAccessFile(fileName, "rw");
randomaccessfile.seek(4L);
byte abyte0[] = new byte[4];
abyte0[0] = (byte)(recCount % 256);
abyte0[1] = (byte)((recCount / 256) % 256);
abyte0[2] = (byte)((recCount / 0x10000) % 256);
abyte0[3] = (byte)((recCount / 0x1000000) % 256);
randomaccessfile.write(abyte0, 0, abyte0.length);
randomaccessfile.close();
}
catch(IOException ioexception)
{
throw new JDBFException(ioexception);
}
}
private BufferedOutputStream stream;
private int recCount;
private JDBField fields[];
private String fileName;
private String dbfEncoding;
}
/**
* <p>Title: java访问DBF文件的接口</p>
* <p>Description: 这个类用于表示DBF文件中的字段</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ict</p>
* @author : He Xiong
* @version 1.0
*/
package com.hexiong.jdbf;
import java.text.*;
import java.util.Date;
// Referenced classes of package com.hexiong.jdbf:
// JDBFException
public class JDBField
{
public JDBField(String s, char c, int i, int j)
throws JDBFException
{
if(s.length() > 10)
throw new JDBFException("The field name is more than 10 characters long: " + s);
if(c != 'C' && c != 'N' && c != 'L' && c != 'D' && c != 'F')
throw new JDBFException("The field type is not a valid. Got: " + c);
if(i < 1)
throw new JDBFException("The field length should be a positive integer. Got: " + i);
if(c == 'C' && i >= 254)
throw new JDBFException("The field length should be less than 254 characters for character fields. Got: " + i);
if(c == 'N' && i >= 21)
throw new JDBFException("The field length should be less than 21 digits for numeric fields. Got: " + i);
if(c == 'L' && i != 1)
throw new JDBFException("The field length should be 1 characater for logical fields. Got: " + i);
if(c == 'D' && i != 8)
throw new JDBFException("The field length should be 8 characaters for date fields. Got: " + i);
if(c == 'F' && i >= 21)
throw new JDBFException("The field length should be less than 21 digits for floating point fields. Got: " + i);
if(j < 0)
throw new JDBFException("The field decimal count should not be a negative integer. Got: " + j);
if((c == 'C' || c == 'L' || c == 'D') && j != 0)
throw new JDBFException("The field decimal count should be 0 for character, logical, and date fields. Got: " + j);
if(j > i - 1)
{
throw new JDBFException("The field decimal count should be less than the length - 1. Got: " + j);
} else
{
name = s;
type = c;
length = i;
decimalCount = j;
return;
}
}
public String getName()
{
return name;
}
public char getType()
{
return type;
}
public int getLength()
{
return length;
}
public int getDecimalCount()
{
return decimalCount;
}
public String format(Object obj)
throws JDBFException
{
if(type == 'N' || type == 'F')
{
if(obj == null)
obj = new Double(0.0D);
if(obj instanceof Number)
{
Number number = (Number)obj;
StringBuffer stringbuffer = new StringBuffer(getLength());
for(int i = 0; i < getLength(); i++)
stringbuffer.append("#");
if(getDecimalCount() > 0)
stringbuffer.setCharAt(getLength() - getDecimalCount() - 1, '.');
DecimalFormat decimalformat = new DecimalFormat(stringbuffer.toString());
String s1 = decimalformat.format(number);
int k = getLength() - s1.length();
if(k < 0)
throw new JDBFException("Value " + number + " cannot fit in pattern: '" + stringbuffer + "'.");
StringBuffer stringbuffer2 = new StringBuffer(k);
for(int l = 0; l < k; l++)
stringbuffer2.append(" ");
return stringbuffer2 + s1;
} else
{
throw new JDBFException("Expected a Number, got " + obj.getClass() + ".");
}
}
if(type == 'C')
{
if(obj == null)
obj = "";
if(obj instanceof String)
{
String s = (String)obj;
if(s.length() > getLength())
throw new JDBFException("'" + obj + "' is longer than " + getLength() + " characters.");
StringBuffer stringbuffer1 = new StringBuffer(getLength() - s.length());
for(int j = 0; j < getLength() - s.length(); j++)
stringbuffer1.append(' ');
return s + stringbuffer1;
} else
{
throw new JDBFException("Expected a String, got " + obj.getClass() + ".");
}
}
if(type == 'L')
{
if(obj == null)
obj = new Boolean(false);
if(obj instanceof Boolean)
{
Boolean boolean1 = (Boolean)obj;
return boolean1.booleanValue() ? "Y" : "N";
} else
{
throw new JDBFException("Expected a Boolean, got " + obj.getClass() + ".");
}
}
if(type == 'D')
{
if(obj == null)
obj = new Date();
if(obj instanceof Date)
{
Date date = (Date)obj;
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
return simpledateformat.format(date);
} else
{
throw new JDBFException("Expected a Date, got " + obj.getClass() + ".");
}
} else
{
throw new JDBFException("Unrecognized JDBFField type: " + type);
}
}
public Object parse(String s)
throws JDBFException
{
s = s.trim();
if(type == 'N' || type == 'F')
{
if(s.equals(""))
s = "0";
try
{
if(getDecimalCount() == 0)
return new Long(s);
else
return new Double(s);
}
catch(NumberFormatException numberformatexception)
{
throw new JDBFException(numberformatexception);
}
}
if(type == 'C')
return s;
if(type == 'L')
{
if(s.equals("Y") || s.equals("y") || s.equals("T") || s.equals("t"))
return new Boolean(true);
if(s.equals("N") || s.equals("n") || s.equals("F") || s.equals("f"))
return new Boolean(false);
else
throw new JDBFException("Unrecognized value for logical field: " + s);
}
if(type == 'D')
{
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
try
{
if("".equals(s))
return null;
else
return simpledateformat.parse(s);
}
catch(ParseException parseexception)
{
throw new JDBFException(parseexception);
}
} else
{
throw new JDBFException("Unrecognized JDBFField type: " + type);
}
}
public String toString()
{
return name;
}
private String name;
private char type;
private int length;
private int decimalCount;
}
/**
* <p>Title: java访问DBF文件的接口</p>
* <p>Description: 这个类用于表示DBF文件中的读写异常</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ict</p>
* @author : He Xiong
* @version 1.0
*/
package com.hexiong.jdbf;
import java.io.PrintStream;
import java.io.PrintWriter;
public class JDBFException extends Exception
{
public JDBFException(String s)
{
this(s, null);
}
public JDBFException(Throwable throwable)
{
this(throwable.getMessage(), throwable);
}
public JDBFException(String s, Throwable throwable)
{
super(s);
detail = throwable;
}
public String getMessage()
{
if(detail == null)
return super.getMessage();
else
return super.getMessage();
}
public void printStackTrace(PrintStream printstream)
{
if(detail == null)
{
super.printStackTrace(printstream);
return;
}
PrintStream printstream1 = printstream;
printstream1.println(this);
detail.printStackTrace(printstream);
return;
}
public void printStackTrace()
{
printStackTrace(System.err);
}
public void printStackTrace(PrintWriter printwriter)
{
if(detail == null)
{
super.printStackTrace(printwriter);
return;
}
PrintWriter printwriter1 = printwriter;
printwriter1.println(this);
detail.printStackTrace(printwriter);
return;
}
private Throwable detail;
}
/**
* <p>Title: java访问DBF文件的接口</p>
* <p>Description: 这个类用于表示DBF文件中的读操作</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ict</p>
* @author : He Xiong
* @version 1.0
*/
package com.hexiong.jdbf;
import java.io.*;
// Referenced classes of package com.hexiong.jdbf:
// JDBFException, JDBField
public class DBFReader
{
public DBFReader(String s)
throws JDBFException
{
stream = null;
fields = null;
nextRecord = null;
try
{
init(new FileInputStream(s));
}
catch(FileNotFoundException filenotfoundexception)
{
throw new JDBFException(filenotfoundexception);
}
}
public DBFReader(InputStream inputstream)
throws JDBFException
{
stream = null;
fields = null;
nextRecord = null;
init(inputstream);
}
private void init(InputStream inputstream)
throws JDBFException
{
try
{
stream = new DataInputStream(inputstream);
int i = readHeader();
fields = new JDBField[i];
int j = 1;
for(int k = 0; k < i; k++)
{
fields[k] = readFieldHeader();
j += fields[k].getLength();
}
if(stream.read() < 1)
throw new JDBFException("Unexpected end of file reached.");
nextRecord = new byte[j];
try
{
stream.readFully(nextRecord);
}
catch(EOFException eofexception)
{
nextRecord = null;
stream.close();
}
}
catch(IOException ioexception)
{
throw new JDBFException(ioexception);
}
}
private int readHeader()
throws IOException, JDBFException
{
byte abyte0[] = new byte[16];
try
{
stream.readFully(abyte0);
}
catch(EOFException eofexception)
{
throw new JDBFException("Unexpected end of file reached.");
}
int i = abyte0[8];
if(i < 0)
i += 256;
i += 256 * abyte0[9];
i = --i / 32;
i--;
try
{
stream.readFully(abyte0);
}
catch(EOFException eofexception1)
{
throw new JDBFException("Unexpected end of file reached.");
}
return i;
}
private JDBField readFieldHeader()
throws IOException, JDBFException
{
byte abyte0[] = new byte[16];
try
{
stream.readFully(abyte0);
}
catch(EOFException eofexception)
{
throw new JDBFException("Unexpected end of file reached.");
}
StringBuffer stringbuffer = new StringBuffer(10);
for(int i = 0; i < 10; i++)
{
if(abyte0[i] == 0)
break;
stringbuffer.append((char)abyte0[i]);
}
char c = (char)abyte0[11];
try
{
stream.readFully(abyte0);
}
catch(EOFException eofexception1)
{
throw new JDBFException("Unexpected end of file reached.");
}
int j = abyte0[0];
int k = abyte0[1];
if(j < 0)
j += 256;
if(k < 0)
k += 256;
return new JDBField(stringbuffer.toString(), c, j, k);
}
public int getFieldCount()
{
return fields.length;
}
public JDBField getField(int i)
{
return fields[i];
}
public boolean hasNextRecord()
{
return nextRecord != null;
}
public Object[] nextRecord()
throws JDBFException
{
if(!hasNextRecord())
throw new JDBFException("No more records available.");
Object aobj[] = new Object[fields.length];
int i = 1;
for(int j = 0; j < aobj.length; j++)
{
int k = fields[j].getLength();
StringBuffer stringbuffer = new StringBuffer(k);
stringbuffer.append(new String(nextRecord, i, k));
aobj[j] = fields[j].parse(stringbuffer.toString());
i += fields[j].getLength();
}
try
{
stream.readFully(nextRecord);
}
catch(EOFException eofexception)
{
nextRecord = null;
}
catch(IOException ioexception)
{
throw new JDBFException(ioexception);
}
return aobj;
}
public void close()
throws JDBFException
{
nextRecord = null;
try
{
stream.close();
}
catch(IOException ioexception)
{
throw new JDBFException(ioexception);
}
}
private DataInputStream stream;
private JDBField fields[];
private byte nextRecord[];
}
最后,给出一个简单的测试样例程序
/**
* <p>Title: java访问DBF文件的接口</p>
* <p>Description: 测试DBF文件的读写</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ict</p>
* @author : He Xiong
* @version 1.0
*/
import com.hexiong.jdbf.DBFReader;
import java.io.PrintStream;
import java.net.URL;
public class Test
{
public Test()
{
}
public static void main(String args[])
throws Exception
{
DBFReader dbfreader = new DBFReader("E:\\lakes.dbf");
int i;
for(i = 0; dbfreader.hasNextRecord(); i++)
{
Object aobj[] = dbfreader.nextRecord();
for (int j=0; j<aobj.length; j++)
System.out.print(aobj[j]+" | ");
System.out.print("\n");
}
System.out.println("Total Count: " + i);
}
}
发表评论
-
命令行快速找出class文件所在的jar包
2012-02-28 18:14 3950for %i in (dir/b/s *.jar) do 7z ... -
常用的数据库连接串(JDBC篇)
2011-12-23 06:58 0看到网上传来传去的jdbc url连接串总结,好多都是粘来粘去 ... -
DBCP连接池的最简单应用(用于ORACLE数据库)
2011-11-19 05:54 4390鉴于有人问起DBCP直接用于JDBC连接的问题,我做了一个最简 ... -
eclipse3.4及以上版本的恢复
2011-08-28 04:58 2289eclipse 3.4以前的版本,如果出现什么问题了,一般都 ... -
简单的整理了一下分页,希望对初学者有帮助
2004-06-23 17:37 694准备工作:在Oracle下,建立一张表:CREATE TABL ... -
编写更快的托管代码[http://blog.csdn.net/zade/category/162553.aspx]
2005-12-14 11:14 950编写更快的托管代 ... -
数据访问技术路线图[摘自MSDN]
2005-12-15 19:04 773数据访问技术路线图 发布日期: 3/22/2005 | 更新 ... -
Jad - the latest version
2006-09-11 12:35 2275转自:http://www.kpdus.com/jad.htm ... -
java 去掉字符串中的'\'字符
2006-11-03 20:00 1052最近发现比较简单的去掉'\'字符居然不能直接用'\'去替。 ... -
python用法: post一个http请求, schedule一个task
2008-02-29 12:29 1586内容在http://iihero.cn上也有,这里转摘一下。近 ... -
用python来解析xml文件(简单情况)
2008-03-02 17:32 1181首先,sax解析最直观,当然,也可以容许xml文件出些错。先给 ... -
在一个目录下边快速创建大量文件及目录(使用dos batch)
2008-03-18 13:01 925@echooffsetnum=100000REMfor/ ... -
如何通过dll文件生成对应的lib文件(开发人员的一个小技巧)
2008-09-28 10:18 941如若转载,请尊重个人劳动,务必注明原始出处。iihero 20 ... -
列一份跨平台开发的读书清单[C/C++ (or Java?)]
2009-03-09 23:20 754如果要开一份跨平台开 ... -
自己动手编译播放器MPC-HC的源码
2009-05-19 21:28 981好久没有关注MPC(Media Player Classic) ... -
Java Performance 总结(1. Class Loader)
2010-06-30 06:50 791关于Java性能方面的东西,涉及挺多。一直想写个总结。 第一 ...
相关推荐
dbf-jdbc-wisecoders,JAVA 读写DBF文件工具包
Java DBF 对dbase文件的读写访问库
Java下读写DBF数据库文件格式研究.pdf
包括java读写dbf文件源码,可改写重用,可用于生成dbf文件或解析dbf文件获取数据。
用JavaDBF操作(读、写)DBF文件
使用这个包提供的API,可以实现对DBF文件的读写
免费资源大家共享
Java下读写DBF数据库文件格式研究
javadbf-0.4.0版本本已增加setCharactersetName方法支持中文读写,但在写入时会发生写入中文数据丢失问题。资源压缩文件包含有修改后的jar文件和读写测试类。 @Test public void testWrite1(){ File file=new File...
最终,像Microsoft Access这样功能更强大的桌面数据库出现了,DBF文件格式仍然是存储和传输数据的最简单方法之一。1.0.0版中的新闻和更改可能的重大更改JavaDBF 1.0.0是第一个稳定的发行版。 它有很多改进,并且...
一般的dbf文件都是数据库操作文件,用c语言进行操作的dll很多,但是java操作的很少,我找到了一个javadbf-0[1].4.0.jar的包,但是操作效果很不理想,尤其是末尾空格和乱码问题,于是我在这个jar包的基础上进行进一步...
java操作dbf文件读写用的jar包。1.9版本。例子: package dbf; import java.io.FileInputStream; import java.io.InputStream; import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFReader; ...
用来读写vf数据库dbf文件的插件,很好用,读数据时没有乱码,写数据时有乱码
Clipper和FoxPro使用xBase DBF文件。 可在Linux(Mono)上编译。 对于.net 4.0项目,有一个枚举框架,在该枚举框架中,可以轻松使用Linq to Objects。 从javadbf派生的代码。 获取二进制文件 从Visual Studio使用...
Shapefile文件是描述空间数据的几何和属性特征的非拓扑实体矢量数据结构的一种格式,由ESRI公司开发。这篇文章给大家介绍了Java如何用GDAL读写shapefile的方法示例,有需要的朋友们可以参考借鉴,下面来一起看看吧。
罗列如下关系型数据库增加了几乎所有基本数据类型和部分复杂类型的支持hdfswriter增加了对十进制数据类型格式的支持hdfswriter增加了对Parquet文件格式的支持hdfswrite增加了目录覆盖模式hdfswriter增加了更多的文件...
一个基于Java的项目,用于在Xbase-DBF(dBase III / Clipper)文件上进行读写。 该项目是JavaDBF的实现:http://sarovar.org/projects/javadbf/积分(JavaDBF):Anil Kumar K. Abraham K. Jacob
2 对数据库文件 (PRC 、 FOX 、 APP 、 DBF), 利用配套软件中的加密工具加密。 3 对于 C 语言及其它编译型语言,配套软件中提供可链接的模块文件 (OBJ 文件 ) , 模块文件中提供两个函数 :(1) 写数据 ; ⑵读数据。...
DBF:dBASE文件,一种由Ashton-Tate创建的格式,可以被ACT!、Lipper、FoxPro、Arago、Wordtech、Xbase和类似数据库或与数据库有关产品识别;可用数据文件(能被Excel 97打开);Oracle 8.1.x表格空间文件 DBX:...