.NET C# - 文本文件中的随机访问 - 不容易? [英] .NET C# - Random access in text files - no easy way?

查看:18
本文介绍了.NET C# - 文本文件中的随机访问 - 不容易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含多个记录".每条记录都包含一个名称和一组数字作为数据.

I've got a text file that contains several 'records' inside of it. Each record contains a name and a collection of numbers as data.

我正在尝试构建一个类,它将读取文件,仅显示所有记录的名称,然后允许用户选择他/她想要的记录数据.

I'm trying to build a class that will read through the file, present only the names of all the records, and then allow the user to select which record data he/she wants.

第一次浏览文件时,我只读取标题名称,但我可以跟踪文件中标题所在的位置".我需要随机访问文本文件以在用户请求后查找每条记录的开头.

The first time I go through the file, I only read header names, but I can keep track of the 'position' in the file where the header is. I need random access to the text file to seek to the beginning of each record after a user asks for it.

我必须这样做,因为文件太大而无法在内存 (1GB+) 中完全读取应用程序的其他内存需求.

I have to do it this way because the file is too large to be read in completely in memory (1GB+) with the other memory demands of the application.

我已尝试使用 .NET StreamReader 类来完成此操作(它提供了非常易于使用的ReadLine"功能,但无法捕获文件的真实位置(BaseStream 属性中的位置偏斜)由于类使用的缓冲区).

I've tried using the .NET StreamReader class to accomplish this (which provides very easy to use 'ReadLine' functionality, but there is no way to capture the true position of the file (the position in the BaseStream property is skewed due to the buffer the class uses).

在 .NET 中没有简单的方法可以做到这一点吗?

Is there no easy way to do this in .NET?

推荐答案

提供了一些很好的答案,但我找不到一些可以在我非常简单的情况下工作的源代码.就在这里,希望它能为其他人节省我花在四处寻找的时间.

There are some good answers provided, but I couldn't find some source code that would work in my very simplistic case. Here it is, with the hope that it'll save someone else the hour that I spent searching around.

我所指的非常简单的情况"是:文本编码是固定宽度的,并且整个文件中的行尾字符是相同的.这段代码在我的情况下运行良好(我正在解析一个日志文件,有时我必须在文件中搜索,然后再回来.我实现了足以做我需要做的事情(例如:只有一个构造函数,并且只覆盖 ReadLine()),所以很可能你需要添加代码......但我认为这是一个合理的起点.

The "very simplistic case" that I refer to is: the text encoding is fixed-width, and the line ending characters are the same throughout the file. This code works well in my case (where I'm parsing a log file, and I sometime have to seek ahead in the file, and then come back. I implemented just enough to do what I needed to do (ex: only one constructor, and only override ReadLine()), so most likely you'll need to add code... but I think it's a reasonable starting point.

public class PositionableStreamReader : StreamReader
{
    public PositionableStreamReader(string path)
        :base(path)
        {}

    private int myLineEndingCharacterLength = Environment.NewLine.Length;
    public int LineEndingCharacterLength
    {
        get { return myLineEndingCharacterLength; }
        set { myLineEndingCharacterLength = value; }
    }

    public override string ReadLine()
    {
        string line = base.ReadLine();
        if (null != line)
            myStreamPosition += line.Length + myLineEndingCharacterLength;
        return line;
    }

    private long myStreamPosition = 0;
    public long Position
    {
        get { return myStreamPosition; }
        set
        {
            myStreamPosition = value;
            this.BaseStream.Position = value;
            this.DiscardBufferedData();
        }
    }
}

以下是如何使用 PositionableStreamReader 的示例:

Here's an example of how to use the PositionableStreamReader:

PositionableStreamReader sr = new PositionableStreamReader("somepath.txt");

// read some lines
while (something)
    sr.ReadLine();

// bookmark the current position
long streamPosition = sr.Position;

// read some lines
while (something)
    sr.ReadLine();

// go back to the bookmarked position
sr.Position = streamPosition;

// read some lines
while (something)
    sr.ReadLine();

这篇关于.NET C# - 文本文件中的随机访问 - 不容易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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