I have a large 20 GB text file with entries resembling entry1MainText:entry1Name
, line separated.
I need to see if a property of an object matches entry1MainText
in any of these lines. So far I have the below code (ref Reading large text files with streams in C#) that reads a line of the file and performs a foreach for said object property. I realise this is likely not the most efficient way.
string file = @"C:\test.txt";
using (FileStream fs = File.Open(file, 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)
{
foreach (UsrFile usrF in rawUsrSorted)
{
if (line.Contains(usrF.Prop1))
{
gridMain.Rows.Add(usrF.Prop1, usrF.Prop2);
}
}
}
}
I do have the benefit of having enough RAM to read the file into memory and parsing there if this would be of benefit, i.e. I have looked a little into MemoryMappedFile
and wonder if this might be of use here.