如何写/读代表对象的二进制文件? [英] How to write/read binary files that represent objects?

查看:83
本文介绍了如何写/读代表对象的二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java编程的新手,但遇到了这个问题:

I'm new to Java programming, and I ran into this problem:

我正在创建一个程序,该程序读取.csv文件,将其行转换为对象,然后操纵这些对象. 更具体地说,应用程序读取每一行并为其指定索引,还从这些行读取某些值并将其存储在TRIE树中. 然后,应用程序可以从树中存储的值中读取索引,然后检索相应行的完整信息.

I'm creating a program that reads a .csv file, converts its lines into objects and then manipulate these objects. Being more specific, the application reads every line giving it an index and also reads certain values from those lines and stores them in TRIE trees. The application then can read indexes from the values stored in the trees and then retrieve the full information of the corresponding line.

我的问题是,即使最近几天我都在研究,我也不知道如何在二进制文件中写入这些结构,也不知道如何读取它们. 我想将这些行(及其索引)写入二进制索引文件中,并且仅读取从TRIEs中检索到的确切索引.

My problem is that, even though I've been researching the last couple of days, I don't know how to write these structures in binary files, nor how to read them. I want to write the lines (with their indexes) in a binary indexed file and read only the exact index that I retrieved from the TRIEs.

对于树的写作,我一直在寻找这样的东西(用C语言编写)

For the tree writing, I was looking for something like this (in C)

fwrite(tree, sizeof(struct TrieTree), 1, file)

对于二进制索引文件",我正在考虑编写诸如TRIEs之类的对象,并可能读取每个对象,直到我读够足以达到相应的索引为止,但这可能不是很有效.

For the "binary indexed file", I was thinking on writing objects like the TRIEs, and maybe reading each object until I've read enough to reach the corresponding index, but this probably wouldn't be very efficient.

概括地说,我需要在读写二进制文件中的对象以及如何创建索引文件的解决方案方面需要帮助.

Recapitulating, I need help in writing and reading objects in binary files and solutions on how to create an indexed file.

推荐答案

我认为(对于初学者而言)尝试序列化时最好.

I think you are (for starters) best off when trying to do this with serialization.

这只是stackoverflow中的一个示例:什么是对象序列化?

Here is just one example from stackoverflow: What is object serialization?

(我认为复制和粘贴代码没有意义,请点击链接阅读)

(I think copy&paste of the code does not make sense, please follow the link to read)

诚然,这还不能解决您的索引创建问题.

Admittedly this does not yet solve your index creation problem.

这篇关于如何写/读代表对象的二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆