21

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

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

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

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

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

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

4

9 回答 9

13

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

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

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 的示例:

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();
于 2009-05-28T15:34:06.503 回答
8

FileStream 有 seek() 方法。

于 2008-11-05T16:15:33.140 回答
5

如果您对数据文件的编写方式很灵活,并且不介意它对文本编辑器不太友好,您可以使用 BinaryWriter 编写记录:

using (BinaryWriter writer = 
    new BinaryWriter(File.Open("data.txt", FileMode.Create)))
{
    writer.Write("one,1,1,1,1");
    writer.Write("two,2,2,2,2");
    writer.Write("three,3,3,3,3");
}

然后,最初读取每条记录很简单,因为您可以使用 BinaryReader 的 ReadString 方法:

using (BinaryReader reader = new BinaryReader(File.OpenRead("data.txt")))
{
    string line = null;
    long position = reader.BaseStream.Position;
    while (reader.PeekChar() > -1)
    {
        line = reader.ReadString();

        //parse the name out of the line here...

        Console.WriteLine("{0},{1}", position, line);
        position = reader.BaseStream.Position;
    }
}

BinaryReader 没有缓冲,因此您可以获得适当的位置以供以后存储和使用。唯一的麻烦是将名称解析出来,无论如何您可能都必须使用 StreamReader。

于 2008-11-05T22:58:08.193 回答
5

您可以使用 System.IO.FileStream 而不是 StreamReader。如果您确切知道包含什么文件(例如编码),您可以像使用 StreamReader 一样进行所有操作。

于 2008-11-05T16:15:17.960 回答
2

编码是固定大小的(例如 ASCII 或 UCS-2)吗?如果是这样,您可以跟踪字符索引(基于您看到的字符数)并根据它找到二进制索引。

否则,不 - 您基本上需要编写自己的 StreamReader 实现,它可以让您查看二进制索引。很遗憾 StreamReader 没有实现这一点,我同意。

于 2008-11-05T16:16:00.237 回答
1

我认为 FileHelpers 库运行时记录功能可能会对您有所帮助。http://filehelpers.sourceforge.net/runtime_classes.html

于 2008-11-05T17:04:17.863 回答
1

一些可能感兴趣的项目。

1)如果行是长度固定的字符集,如果字符集具有可变大小(如 UTF-8),则这不是必要的有用信息。所以检查你的字符集。

2)您可以通过使用 BaseStream.Position 值从 StreamReader 确定文件光标的确切位置,如果您首先 Flush() 缓冲区(这将强制当前位置成为下一次读取将开始的位置 - 最后一个字节后字节读取)。

3)如果您事先知道每条记录的确切长度将是相同的字符数,并且字符集使用固定宽度的字符(因此每行的字节数相同),您可以使用 FileStream 与固定缓冲区大小以匹配行的大小,并且每次读取结束时光标的位置将是下一行的开始。

4)如果行的长度相同(假设在此处以字节为单位),您是否不简单地使用行号并根据行大小 x 行号计算文件中的字节偏移量,是否有任何特殊原因?

于 2010-03-29T13:57:20.130 回答
0

This exact question was asked in 2006 here: http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic40275.aspx

Summary:

"The problem is that the StreamReader buffers data, so the value returned in BaseStream.Position property is always ahead of the actual processed line."

However, "if the file is encoded in a text encoding which is fixed-width, you could keep track of how much text has been read and multiply that by the width"

and if not, you can just use the FileStream and read a char at a time and then the BaseStream.Position property should be correct

于 2008-11-05T17:44:43.053 回答
0

您确定文件“太大”吗?您是否尝试过这种方式并引起了问题?

如果您分配了大量内存,而您现在没有使用它,Windows 只会将其换出到磁盘。因此,通过从“内存”访问它,您将完成您想要的——随机访问磁盘上的文件。

于 2008-11-05T16:15:24.717 回答