如何从末尾读取序列化文件 [英] how to read from a serialized file from end

查看:116
本文介绍了如何从末尾读取序列化文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友:

我想从序列化文件的末尾读取.我的文件具有如下所示的形状.我从文件末尾读取每个对象的大小,然后将光标放置在我最喜欢的对象中.object 1:
a1
a2
a3
.
.
.
对象2:
b1
b2
b3
.
.
.


EOF%10 30 EOF%


如何从文件末尾读取两个字段,然后调整光标的位置?

Yours

Dear buddies:

I want to read from the end of a file that is serialize. my file has some shapes like below. I read the size of each object from the end of the file and then set the position of the cursor in the my favorite object.object 1:
a1
a2
a3
.
.
.
object 2:
b1
b2
b3
.
.
.


EOF% 10 30 EOF%


how can I read two fields from the end of file and then adjusting the position of the cursor?

Yours

推荐答案

听起来像您需要将整个文件读入内存,然后从背面开始工作.从外观上看,也许可以使用File.ReadAllLines( http://msdn .microsoft.com/en-us/library/system.io.file.readalllines.aspx [
Sounds like you need to read the entire file into memory, and then work from the back. From what it looks like, maybe can use File.ReadAllLines (http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^])

You can read the last line by finding the length of the array, and go from there.


文本文件和行的问题在于行的长度不同,因此可以考虑使用作为字符数组的锯齿状数组".如果不扫描所有文件,则永远无法预测最后一行的位置.

如果文件很大,我只能看到以下解决方法:至少使用System.IO.StreamReader.ReadLine
^ ].扔掉行内容,只记住文件中的位置和(可选)每行的长度.将此信息放在一些数组中,这些数组将起到文件映射的作用.如果即使对于这样的映射,该数据也太大,则将该数据写入二进制文件.由于该文件是二进制文件,因此每个记录的大小都相同,因此您可以使用对该文件的随机访问读取.这样,从原始输入文件中读取一行将分两步:首先,在所需索引处读取地图记录.为此,将Stream.Position设置为sizeof(metadataRecord) + index * sizeof(mapRecord),其中metadataRecord可以包含一些固定大小的元数据(例如,行总数),而mapRecord是关于每行位置/大小的记录在输入文件中.第二步,在mapRecord的输入文件中设置Position并从这一点开始读取.我建议保持输入文件以二进制模式打开.使用使用Stream.Position http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx [
The problem with text files and line is that the lines have different length, so it could be considered as a "jagged array" of arrays of characters. You can never predict the position of last line if you did not scan all the file.

If the file is big, the only work-around I can see is this: at least once scan the input file from the beginning to the end using System.IO.StreamReader.ReadLine, http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx[^]. Throw out the line contents, only remember position in the file and (optionally) the length of each line. Put this information in some array which will play a role of the file map. If this data is too big even for such a map, write this data in a binary file. As the file is binary, each record takes the same size, so you can use random-access read of this file. This way, reading a line from an original input file will be two-step: first, read the map record at required index. For this purpose, set Stream.Position to sizeof(metadataRecord) + index * sizeof(mapRecord), where metadataRecord could contain some fixed-size meta-data (for example, total number of lines), and mapRecord is the record about position/size of each line in the input file. On a second step, set Position in the input file from mapRecord and read from this point. I would advise to keep the input file open in binary mode. Use the class System.IO.BinaryReader and its property BaseStream needed to work with Stream.Position, http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].

—SA


这篇关于如何从末尾读取序列化文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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