DBASE DBF数据库文件解析

基于Java实现DBase DBF文件的解析和显示

JDK19编译运行,实现了数据库字段和数据解析显示。

首先解析数据库文件头代码

java 复制代码
		byte bytes[] = Files.readAllBytes(Paths.get(file));
		BinaryBufferArray bis = new BinaryBufferArray(bytes);
		
		DBF dbf = new DBF();
		dbf.VersionNumber = bis.ReadUInt8();		// 版本号
		dbf.DateOfLastUpdate = bis.ReadBytes(3);  	// 最后更新日期
		dbf.NumberOfRecords = bis.ReadUInt32();		// 记录数量
		dbf.LengthOfHeaderStructure = bis.ReadUInt16();	// 文件头长(内容开始位置)
		dbf.LengthOfEachRecord  = bis.ReadUInt16();	// 每条记录长度
		short Reserved = bis.ReadInt16();
		dbf.IncompleteTransac = bis.ReadUInt8();	
		dbf.EncryptionFlag = bis.ReadUInt8();
		dbf.FreeRecordThread = bis.ReadInt32();
		long ReservedForMultiUser= bis.ReadInt64();
		dbf.MDXFlag = bis.ReadUInt8();
		dbf.LanguageDriver = bis.ReadUInt8();	
		Reserved = bis.ReadInt16();

解析字段代码

java 复制代码
		dbf.fields = new Vector<Field>();
		while (true) {
			byte _b = bis.ReadInt8();
			
			if (_b == 0x0d) 	// 是否结束
				break ;
			
			byte[] bs = bis.ReadInt8(31);
			byte[] all = new byte[32];
			all[0] = _b;
			System.arraycopy(bs, 0, all, 1, 31);
			
			Field field = Field.parse(all);	
			dbf.fields.add(field);
		}
java 复制代码
	static Field parse(byte[] b) throws IOException  {
		Field field = new Field();
		BinaryBufferArray t = new BinaryBufferArray(b);
		field.name = t.ReadAsciiString(11).trim();	//字段名称
		field.type = t.ReadAsciiChar();				// 字段类型
		t.SkipBytes(4);
		field.length = t.ReadUInt8();				// 字段长度
		field.precision = t.ReadUInt8();			// 精度
		t.SkipBytes(2);
		field.id = t.ReadUInt8();
		t.SkipBytes(10);
		field.mdx = t.ReadUInt8();
		return field;
	}

解析数据代码:

java 复制代码
		dbf.resultset = new Vector<Map<String, Object>>();	//结果集
		for (int i=0; i<dbf.NumberOfRecords-1; i++) {
			
			char delete = '\0';		
			delete = bis.ReadAsciiChar();	// 读入删除标记
			
			HashMap<String,Object> map = new HashMap<>();
			for (int j=0; j<dbf.fields.size(); j++) {
				Field field = dbf.fields.get(j);
				char type = field.getType();
				int len = field.getLength();
				Object val = null;
				
				byte[] b = bis.ReadBytes(len);	//读取字段值
				if (field.getType() == 'N')
					val = new String(b);
				else if (field.getType() =='C')
					val = new String(b, "UTF-8");
				
				map.put(field.getName(), val);
			}
			
			map.put("delete", delete);
			dbf.resultset.add(map);
		}

读入dbf文件,解析显示如下:

相关推荐
欧先生^_^6 分钟前
Linux内核可配置的参数
linux·服务器·数据库
问道飞鱼7 分钟前
【数据库知识】Mysql进阶-高可用MHA(Master High Availability)方案
数据库·mysql·adb·高可用·mha
tiging8 分钟前
centos7.x下,使用宝塔进行主从复制的原理和实践
数据库·mysql·adb·主从复制
wangcheng869936 分钟前
Oracle常用函数-日期时间类型
数据库·sql·oracle
zizisuo39 分钟前
面试篇:Spring Security
网络·数据库·安全
一只fish1 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(2)
数据库·mysql
StarRocks_labs1 小时前
从InfluxDB到StarRocks:Grab实现Spark监控平台10倍性能提升
大数据·数据库·starrocks·分布式·spark·iris·物化视图
搞不懂语言的程序员1 小时前
Redis的Pipeline和Lua脚本适用场景是什么?使用时需要注意什么?
数据库·redis·lua
王RuaRua1 小时前
[数据结构]5. 栈-Stack
linux·数据结构·数据库·链表
Lw老王要学习3 小时前
Linux数据库篇、第一章_02_MySQL的使用增删改查
linux·运维·数据库·mysql·云计算·it