Bootstrap

DBASE DBF数据库文件解析

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

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

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

		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();

解析字段代码

		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);
		}
	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;
	}

解析数据代码:

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

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;