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文件,解析显示如下:

相关推荐
小蒜学长19 小时前
基于Spring Boot的火灾报警系统的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
福赖19 小时前
《MySQL基础——C 语言链接》
c语言·数据库·mysql
KIDAKN19 小时前
Redis 分布式锁
数据库·redis·分布式
程序新视界19 小时前
如何为MySQL中的JSON字段设置索引
数据库·mysql
Ultipa19 小时前
查询语言的进化:SQL之后,为什么是GQL?数据世界正在改变
数据库·sql·图数据库·gql
LB211219 小时前
SQL隐式链接显式连接
大数据·数据库·sql
隔壁阿布都20 小时前
spring boot + mybatis 使用线程池异步修改数据库数据
数据库·spring boot·mybatis
MAGICIAN...1 天前
【Redis】--持久化机制
数据库·redis·缓存
我真的是大笨蛋1 天前
JVM调优总结
java·jvm·数据库·redis·缓存·性能优化·系统架构
步步为营DotNet1 天前
5-2EFCore性能优化
数据库·性能优化·.net