我有一个大小为50GB及以上的Json文件。 以下是我写的阅读Json的一小部分内容。我现在需要修改它来读取大文件。在C#中高效地读取极大文件。目前使用StreamReader
internal static IEnumerable<T> ReadJson<T>(string filePath)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (StreamReader sr = new StreamReader(filePath))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
byte[] jsonBytes = Encoding.UTF8.GetBytes(line);
XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max);
var myPerson = ser.ReadObject(jsonReader);
jsonReader.Close();
yield return (T)myPerson;
}
}
}
- 想如果我指定缓冲区大小,而在当前代码构建的StreamReader就足够了?
- 如果我在这里错了,请纠正我。缓冲区大小基本上指定了一次从磁盘读取多少数据到内存。因此,如果文件大小为100MB,缓冲区大小为5MB,则每次读取内存5MB,直到读取完整个文件。
- 假设我对第3点的理解是正确的,那么对于如此大的文本文件,理想的缓冲区大小是多少? int.Max大小是一个坏主意?在64位PC中,int.Max大小为2147483647.我认为缓冲区大小是以字节为单位的,估计大约为2GB。这本身可能会浪费时间。我一直在寻找像100MB - 300MB这样的缓冲区大小。
增加缓冲区的大小,甚至低至128K不太可能有很多好处。 1MB缓冲区已经比它需要的大。然而,确保唯一的方法是使用不同的缓冲区大小。 –
50GB文件?如果是信用卡一览表可我有一个副本,请(JK) –