gpt4 book ai didi

c# - 将大文本文件加载到字符串中

转载 作者:行者123 更新时间:2023-11-30 14:53:49 24 4
gpt4 key购买 nike

我想将一个 150 MB 的文本文件加载到一个字符串中。该文件是 UTF16 编码的,因此它会在内存中生成一个大约 150 MB 的字符串。我尝试过的所有方法都会导致内存不足异常。

我知道这是一个巨大的字符串,当然不是我想做的事情。但是,如果不对即将推出的应用程序进行大量真正深入的更改,目前我真的无能为力。该文件中没有均匀分布的行集。一行可以包含整个文件大小的 80% 左右。

这是我尝试过的:

方法一

// Both of these throw Out of Memory exception
var s = File.ReadAllText(path)
var s = File.ReadAllText(path, Encoding.Unicode);

方法二

var sb = new StringBuilder();

// I've also tried a few other iterations on this with other types of streams
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}

// This throws an exception
sb.ToString();

方法三

using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs, Encoding.Unicode))
{
int initialSize = (int)fs.Length / 2; // Comes to a value of 73285158 with my test file
var sb = new StringBuilder(initialSize); // This throws an exception

string line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}

sb.ToString();
}

那么,我该怎么做才能将这个文件加载到一个字符串变量中呢?

编辑:添加了额外的尝试以根据评论解决问题。

最佳答案

到目前为止,您的两次尝试都将文件视为 UTF-8 格式。在最好的情况下,这将占用两倍的内存——而且它很可能基本上是无效数据(如 UTF-8)。您应该尝试指定编码:

var text = File.ReadAllText(path, Encoding.Unicode);

如果这不起作用,您可以在第二个代码上尝试变体,但将编码指定为 StreamReader (并且可能会忽略 BufferedStream - 我认为这对您没有帮助),并且还指定了 StringBuilder 的初始容量。 , 等于文件大小的一半。

编辑:如果此行抛出异常:

var sb = new StringBuilder(initialSize);

...那你就没有机会了。您无法分配足够的连续内存。

可能发现您能够使用 List<string>相反:

var lines = File.ReadLines(path).ToList();

... 至少你有很多对象。它将占用更多内存,但不需要那么多的连续内存。这是假设您一次确实需要内存中的整个文件。如果您可以改为流式传输数据,那将是一个更好的选择。

在一个小型控制台应用程序中,我可以使用 File.ReadAllText 毫无问题地读取相同大小的文件。 ,同时使用 32 位和 64 位 CLR...所以这可能与您的物理内存以及您在程序中执行的其他操作有关。

关于c# - 将大文本文件加载到字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com