.NET C# - 在文本文件中随机访问 - 没有简单的方法? [英] .NET C# - Random access in text files - no easy way?

查看:130
本文介绍了.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.

我想建立一个类,将通过文件,present只读的所有记录的名称,然后让用户选择他/她想要哪些记录数据。

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?

推荐答案

有提供了一些很好的答案,但我无法找到一些根源$ C ​​$ C,将在我的非常简单的情况下工作。这就是,希望它会拯救别人,我花了四周搜索小时。

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.

在非常简单的情况下,我指的是:文本编码是固定的宽度,而行结束字符在整个文件中的相同。这code在我的情况下效果很好(在这里我解析一个日志文件,我有时在文件中寻求前进,然后回来我实现刚好够做我需要做的(例如:只有一个构造函数,只有重写的ReadLine()),所以最有可能你需要添加code ...但我认为这是一个合理的起点。

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天全站免登陆