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

相关推荐
已是上好佳38 分钟前
介绍一下Qt 中的QSizePolicy 布局策略
数据库·qt
钡铼技术物联网关1 小时前
导轨式ARM工业控制器:组态软件平台的“神经中枢”
linux·数据库·人工智能·安全·智慧城市
驜鸈1 小时前
MySQL 的EXPLAIN 计划 type 字段详细说明
android·数据库·mysql
XiYang-DING1 小时前
【数据库系统概论】第一章 绪论
数据库·oracle
码上飞扬1 小时前
Oracle RAC配置原理详解:构建高可用与高性能的数据库集群
数据库·oracle·rac
小刘爱喇石( ˝ᗢ̈˝ )1 小时前
行式数据库与列式数据库区别
数据库·分布式
编程修仙1 小时前
数据库批处理
数据库·oracle
hjehheje1 小时前
clickhouse删除一条数据
数据库·clickhouse·oracle
yukai080081 小时前
【最后203篇系列】011 Mongo异步代理开发回顾
数据库·oracle
ianozo1 小时前
二次SQL注入
数据库·sql