Snippet - Read StreamReader Contents In Chunks - ArkPhaze - 08-20-2012
Here's a cool snippet I came up with to read using a StreamReader in chunks based on an input of how many lines you want to read at a time until the end of the Stream for the file.
Good example usage for where the null coalescing operator in C# comes in nicely as well :ok:
I used the fully fleged StringBuilder just in case we're dealing with a fair bit of lines based on the lnBufferCount for determining the buffer size for the string we're dealing with through the loop.
Code: private void MainMethod()
{
using (StreamReader sr = new StreamReader(@"F:\file.txt"))
{
string lnBuffer = string.Empty;
int lnBufferCount = 5;
//If based on user input:
if (lnBufferCount < 1) throw new ArgumentException("lnBufferCount cannot be less than 1.");
while (LinesAvailable(ref lnBuffer, ReadLines(lnBufferCount, sr)))
{
MessageBox.Show(lnBuffer);
}
}
}
private bool LinesAvailable(ref string lnBuffer, string readLine)
{
lnBuffer = readLine;
return !(lnBuffer.StartsWith("\0"));
}
private string ReadLines(int numLines, StreamReader reader)
{
StringBuilder sb = new StringBuilder();
Array.ForEach(Enumerable.Range(1, numLines).ToArray(), i => sb.AppendLine(reader.ReadLine() ?? "\0"));
return sb.ToString();
}
Enjoy
RE: Snippet - Read StreamReader Contents In Chunks - ArkPhaze - 08-20-2012
So say we had a file like:
Code: Name: ArkPhaze
UID: 1234
Group: Regular
Name: Frooxious
UID: 234134
Group: Regular
Name: 1llusion
UID: 64445
Group: Admin
Name: bluedog.tar.gz
UID: 77777
Group: Admin
Now I can read off the data from this file in chunks of linecounts of 4 in length, which enables me to deal with these string values each time through the loop, and apply my standard action to each:
Code: Name: ArkPhaze
UID: 1234
Group: Regular
Code: Name: Frooxious
UID: 234134
Group: Regular
Code: Name: 1llusion
UID: 64445
Group: Admin
Code: Name: bluedog.tar.gz
UID: 77777
Group: Admin
Example: (Using the file above)
Code: private void MainMethod()
{
using (StreamReader sr = new StreamReader(@"F:\file.txt"))
{
string lnBuffer = string.Empty;
int lnBufferCount = 4;
//If based on user input:
if (lnBufferCount < 1) throw new ArgumentException("lnBufferCount cannot be less than 1.");
while (LinesAvailable(ref lnBuffer, ReadLines(lnBufferCount, sr)))
{
string[] lines = lnBuffer.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
string[] data = lines.Take(3).Select(s => s.Split(':')[1].Trim()).ToArray();
MessageBox.Show(string.Format("The member {0} has a UID of {1} and is in the {2}'s group.", data[0], data[1], data[2]));
}
}
}
private bool LinesAvailable(ref string lnBuffer, string readLine)
{
lnBuffer = readLine;
return !(lnBuffer.StartsWith("\0"));
}
private string ReadLines(int numLines, StreamReader reader)
{
StringBuilder sb = new StringBuilder();
Array.ForEach(Enumerable.Range(1, numLines).ToArray(), i => sb.AppendLine(reader.ReadLine() ?? "\0"));
return sb.ToString();
}
Result:
![[Image: EOQOI.png]](http://i.imgur.com/EOQOI.png)
![[Image: vQOej.png]](http://i.imgur.com/vQOej.png)
![[Image: 5WHcz.png]](http://i.imgur.com/5WHcz.png)
![[Image: fAegn.png]](http://i.imgur.com/fAegn.png)
You could even have used Regex.
Not too good with Regex, but here's an example:
Code: Match m = Regex.Match(lnBuffer, @"\bName: (.*?)\r\nUID: (.*?)\r\nGroup: (\w.*?)\r?\n?\b");
MessageBox.Show(string.Format("The member {0} has a UID of {1} and is in the {2}'s group.", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value));
Inside the while loop.
|