逐块读取文本文件

发布于 2024-12-27 15:56:27 字数 135 浏览 0 评论 0原文

我正在使用 C#.net

如何逐块读取文本文件,块由换行符分隔。

块大小不固定,因此我无法使用 StreamReader 的 ReadBlock 方法。

是否有其他方法可以逐块获取数据,因为数据由换行符分隔。

I am using C#.net

How can read a text file block by block, the block is separated by new line character.

Block size is not fixed, so i am not able to use ReadBlock method of StreamReader.

Is there any ohter method for getting the data block by block as it is separated by new line character.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

李白 2025-01-03 15:56:27

您可以使用 StreamReader

using (var reader = File.OpenText("foo.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // do something with the line
    }
}

此方法读取文本逐行文件(其中Environment.NewLine用作行分隔符)并且一次仅将当前行加载到内存中,因此它可用于读取非常大的文件。

如果您只想将小文本文件的所有行加载到内存中,您也可以使用 ReadAllLines 方法:

string[] lines = File.ReadAllLines("foo.txt");
// the lines array will contain all the lines
// don't use this method with large files
// as it loads the entire contents into memory

You could use a StreamReader:

using (var reader = File.OpenText("foo.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // do something with the line
    }
}

This method reads the text file line by line (where Environment.NewLine is used as line separator) and has only the current line loaded in memory at once so it could be used to read very large files.

If you just want to load all the lines of a small text file in memory you could also use the ReadAllLines method:

string[] lines = File.ReadAllLines("foo.txt");
// the lines array will contain all the lines
// don't use this method with large files
// as it loads the entire contents into memory
挽手叙旧 2025-01-03 15:56:27

您可以查看 StreamReader.ReadToEnd() 和 String.Split()

的使用:

string content = stream.ReadToEnd();
string[] blocks = content.Split('\n');//You may want to use "\r\n"

You can look at StreamReader.ReadToEnd() and String.Split()

use:

string content = stream.ReadToEnd();
string[] blocks = content.Split('\n');//You may want to use "\r\n"
最舍不得你 2025-01-03 15:56:27

您可以使用 File.ReadLines 方法

foreach(string line in File.ReadLines(path))
{
   //do something with line
}

ReadLines 返回一个 IEnumerable,因此一次只有一行存储在内存中。

You could use the File.ReadLines method

foreach(string line in File.ReadLines(path))
{
   //do something with line
}

ReadLines returns an IEnumerable<string> so only one line is stored in memory at once.

跨年 2025-01-03 15:56:27

类似这样的事情:

using (TextReader tr = new StreamReader(FullFilePath))
{
  string Line;
  while ((Line = tr.ReadLine()) != null)
  {
    Line = Line.Trim();
  }
}

有趣的是,前几天我正在做一些文件 i/o,发现 this 这非常有用处理大型分隔记录文本文件(例如,将记录转换为您自己定义的预定义类型)

Something like:

using (TextReader tr = new StreamReader(FullFilePath))
{
  string Line;
  while ((Line = tr.ReadLine()) != null)
  {
    Line = Line.Trim();
  }
}

Funny I was doing some file i/o the other day, and found this which was very useful at processing large delimited record text files (e.g. converting a record to a pre-defined type which you define yourself)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文