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

相关推荐
一 乐14 分钟前
基于vue船运物流管理系统设计与实现(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端·船运系统
jerry60941 分钟前
注解(Annotation)
java·数据库·sql
AIGC大时代4 小时前
对比DeepSeek、ChatGPT和Kimi的学术写作撰写引言能力
数据库·论文阅读·人工智能·chatgpt·数据分析·prompt
如风暖阳4 小时前
Redis背景介绍
数据库·redis·缓存
lingllllove5 小时前
Redis脑裂问题详解及解决方案
数据库·redis·缓存
字节全栈_BjO5 小时前
mysql死锁排查_mysql 死锁问题排查
android·数据库·mysql
微光守望者5 小时前
Redis常见命令
数据库·redis·缓存
martian6656 小时前
第六篇:事务与并发控制
数据库
x-cmd8 小时前
[250202] DocumentDB 开源发布:基于 PostgreSQL 的文档数据库新选择 | Jekyll 4.4.0 发布
数据库·postgresql·开源