您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

TinyDBF-200行写个DBF解析器

2026/2/10 4:55:55发布5次查看
序言 由于工作关系,需要工作当中,需要读取dbf文件,找了一些dbf读取开源软件,要么是太过庞大,动不动就上万行,要么是功能有问题,编码,长度,总之是没有找到一个非常爽的。在万般无奈之下,我老人家怒从心头起,恶向胆边生,决定自己写一下。结果只用了
序言由于工作关系,需要工作当中,需要读取dbf文件,找了一些dbf读取开源软件,要么是太过庞大,动不动就上万行,要么是功能有问题,编码,长度,总之是没有找到一个非常爽的。在万般无奈之下,我老人家怒从心头起,恶向胆边生,决定自己写一下。结果只用了不到300行代码就搞定了,当然搞定不是唯一目标,还要优雅简洁的搞定,亲们跟随我的脚步一起感受一下简洁的设计与实现吧。
在开始编码之前,先介绍一下dbf,这个dbf可是个老东西,在dos时代就已经出现,并且风骚了相当一段时间,后来随着大型数据库的应用,它逐步没落,但是由于其简洁易用的特点,还是应用在大量的数据交换当中。但是其发展过程中,也形成了许多种版本,不同版本的结构不一样,也就决定 了其解析程序也是不一样的。
今天我只实现了foxbase/dbaseiii的解析,但是也为扩展各种其它版本做好了准备。
接口设计
上面一共就两个类,一个接口,field和header就是两个简单的pojo类,分别定义了文件头及字段相关的信息。
reader接口是dbf文件读取的接口,主要定义了获取文件类型,编码,字段以及记录移动相关的方法。
代码实现首先实现reader的抽象类
public abstract class dbfreader implements reader { protected string encode = gbk; private filechannel filechannel; protected header header; protected list fields; private boolean recordremoved; int position = 0; static map readermap = new hashmap(); static { addreader(3, foxprodbase3reader.class); } public static void addreader(int type, class clazz) { readermap.put(type, clazz); } public static void addreader(int type, string classname) throws classnotfoundexception { readermap.put(type, class.forname(classname)); } public byte gettype() { return 3; } public string getencode() { return encode; } public header getheader() { return header; } public list getfields() { return fields; } public boolean isrecordremoved() { return recordremoved; } public static reader parse(string dbffile, string encode) throws ioexception, illegalaccessexception, instantiationexception { return parse(new file(dbffile), encode); } public static reader parse(string dbffile) throws ioexception, illegalaccessexception, instantiationexception { return parse(new file(dbffile), gbk); } public static reader parse(file dbffile) throws ioexception, illegalaccessexception, instantiationexception { return parse(dbffile, gbk); } public static reader parse(file dbffile, string encode) throws ioexception, illegalaccessexception, instantiationexception { randomaccessfile afile = new randomaccessfile(dbffile, r); filechannel filechannel = afile.getchannel(); bytebuffer bytebuffer = bytebuffer.allocate(1); filechannel.read(bytebuffer); byte type = bytebuffer.array()[0]; class readerclass = readermap.get((int) type); if (readerclass == null) { filechannel.close(); throw new ioexception(不支持的文件类型[ + type + ]。); } dbfreader reader = (dbfreader) readerclass.newinstance(); reader.setfilechannel(filechannel); reader.readheader(); reader.readfields(); return reader; } public void setfilechannel(filechannel filechannel) { this.filechannel = filechannel; } protected abstract void readfields() throws ioexception; public void movebeforefirst() throws ioexception { position = 0; filechannel.position(header.getheaderlength()); } /** * @param position 从1开始 * @throws java.io.ioexception */ public void absolute(int position) throws ioexception { checkposition(position); this.position = position; filechannel.position(header.getheaderlength() + (position - 1) * header.getrecordlength()); } private void checkposition(int position) throws ioexception { if (position >= header.getrecordcount()) { throw new ioexception(期望记录行数为 + (this.position + 1) + ,超过实际记录行数: + header.getrecordcount() + 。); } } protected abstract field readfield() throws ioexception; protected abstract void readheader() throws ioexception; private void skipheaderterminator() throws ioexception { bytebuffer bytebuffer = bytebuffer.allocate(1); readbytebuffer(bytebuffer); } public void close() throws ioexception { filechannel.close(); } public void next() throws ioexception { checkposition(position); bytebuffer bytebuffer = bytebuffer.allocate(1); readbytebuffer(bytebuffer); this.recordremoved = (bytebuffer.array()[0] == '*'); for (field field : fields) { read(field); } position++; } public boolean hasnext() { return position < header.getrecordcount(); } private void read(field field) throws ioexception { bytebuffer buffer = bytebuffer.allocate(field.getlength()); readbytebuffer(buffer); field.setstringvalue(new string(buffer.array(), encode).trim()); field.setbuffer(buffer); } protected void readbytebuffer(bytebuffer bytebuffer) throws ioexception { filechannel.read(bytebuffer); }}
这个类是最大的一个类,值得注意的是几个静态方法: addreader和parse,addreader用于增加新的类型的reader,parse用于解析文件。
parse的执行过程是首先读取第一个字节,判断是否有对应的解析实现类,如果有,就有对应的解析实现类去解析,如果没有,则抛出错误声明不支持。
下面写实现类就简单了,下面是foxprodbase3的解析器:
public class foxprodbase3reader extends dbfreader { protected void readfields() throws ioexception { fields = new arraylist(); for (int i = 0; i < (header.getheaderlength() - 32 - 1) / 32; i++) { fields.add(readfield()); } } public byte gettype() { return 3; } protected field readfield() throws ioexception { field field = new field(); bytebuffer bytebuffer = bytebuffer.allocate(32); readbytebuffer(bytebuffer); byte[] bytes = bytebuffer.array(); field.setname(new string(bytes, 0, 11, encode).trim().split(\0)[0]); field.settype((char) bytes[11]); field.setdisplacement(util.getunsignedint(bytes, 12, 4)); field.setlength(util.getunsignedint(bytes, 16, 1)); field.setdecimal(util.getunsignedint(bytes, 17, 1)); field.setflag(bytes[18]); return field; } protected void readheader() throws ioexception { header = new header(); bytebuffer bytebuffer = bytebuffer.allocate(31); readbytebuffer(bytebuffer); byte[] bytes = bytebuffer.array(); header.setlastupdate((util.getunsignedint(bytes, 0, 1) + 1900) * 10000 + util.getunsignedint(bytes, 1, 1) * 100 + util.getunsignedint(bytes, 2, 1)); header.setrecordcount(util.getunsignedint(bytes, 3, 4)); header.setheaderlength(util.getunsignedint(bytes, 7, 2)); header.setrecordlength(util.getunsignedint(bytes, 9, 2)); }}
测试用例public class dbfreadertest { static string[] files = {bestimate20140401, bhdquote20140401}; public static void main(string[] args) throws ioexception, illegalaccessexception, instantiationexception { for (string file : files) { printfile(file); } } public static void printfile(string filename) throws ioexception, instantiationexception, illegalaccessexception { reader dbfreader = dbfreader.parse(e:\\20140401\\ + filename + .dbf); for (field field : dbfreader.getfields()) { system.out.printf(name:%s %s(%d,%d)\n, field.getname(), field.gettype(), field.getlength(), field.getdecimal()); } system.out.println(); for (int i = 0; i < dbfreader.getheader().getrecordcount(); i++) { dbfreader.next(); for (field field : dbfreader.getfields()) { system.out.printf(% + field.getlength() + s, field.getstringvalue()); } system.out.println(); } dbfreader.close(); }}
可以看到最后的使用也是非常简洁的。
代码统计
总共的代码行数是282行,去掉import和接口声明之类的,真正干活的代码大概就200行了:
总结上面不仅展示了如何实现dbf文件的解析,同时还展示了如何在现在面临的需求与未来的扩展进行合理均衡的设计方式。
比如:要实现另外一个标准的dbf文件支持,只要类似上面foxprodbase3reader类一样,简单实现之后,再调用dbfparser.addreader(xxxreader);
好的设计需要即避免过度设计,搞得太复杂,同时也要对未来的变化与扩展做适当考虑,避免新的需求来的时候需要这里动动,那里改改导致结构上的调整与变化,同时要注意遵守dry原则,可以这样说如果程序中有必要的大量的重复,就说明一定存在结构设计上的问题。
所有的代码都可以在下面的连接看到:
https://code.csdn.net/tinygroup/tiny/tree/master/framework/org.tinygroup.dbf
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product