ReadAllLines for Stream object?

There is File.ReadAllLines , but not Stream.ReadAllLines .

 using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Test_Resources.Resources.Accounts.txt")) using (StreamReader reader = new StreamReader(stream)) { // Would prefer string[] result = reader.ReadAllLines(); string result = reader.ReadToEnd(); } 

Is there a way to do this or do I need to manually scroll the file line by line?

+50
c # file stream text
Nov 09 '12 at 17:23
source share
6 answers

You can write a method that reads line by line, for example:

 public IEnumerable<string> ReadLines(Func<Stream> streamProvider, Encoding encoding) { using (var stream = streamProvider()) using (var reader = new StreamReader(stream, encoding)) { string line; while ((line = reader.ReadLine()) != null) { yield return line; } } } 

Then name it like:

 var lines = ReadLines(() => Assembly.GetExecutingAssembly() .GetManifestResourceStream(resourceName), Encoding.UTF8) .ToList(); 

The Func<> should handle reading more than once and avoid unnecessary threads. Of course, you can easily wrap this code in a method.

If you donโ€™t need all at once in memory, you donโ€™t even need ToList ...

+89
Nov 09 '12 at 17:27
source share

The .EndOfStream property can be used in a loop instead of checking whether the next line is the next.

 List<string> lines = new List<string>(); using (StreamReader reader = new StreamReader("example.txt")) { while(!reader.EndOfStream) { lines.Add(reader.ReadLine()); } } 
+32
Dec 12 '13 at 16:04
source share
 using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Test_Resources.Resources.Accounts.txt")) using (StreamReader reader = new StreamReader(stream)) { // Would prefer string[] result = reader.ReadAllLines(); string[] result = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); } 
+6
Nov 09 '12 at 17:27
source share

Using Split here:

 reader .ReadToEnd() .Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 

not equivalent to ReadLine . If you look at the ReadLine source, StreamReader.cs , you will see that it handles the line terminators: \ r, \ n and \ r \ n correctly. ReadLine does not return an extra empty line when the line terminator is \ r \ n, which is typical for DOS / Windows. Split "sees" (parses) \ r, followed by \ n as two separate delimiters, and returns an empty string.

'StringSplitOptions.RemoveEmptyEntries' in the above code removes these empty lines, but also removes any empty lines that also appear in the input.

Thus, to enter line1 \ r \ p line3 \ r ReadLine returns 3 lines. The second is empty. Split creates 4 lines. (After the last \ r. There is an additional line). Then he removes the 2nd and 4th.

Please note that Split not suitable for parsing text strings that are "post-service". That is, the separator appears after the token. So far, Split is suitable for infix, where separators appear between tokens. This is the difference between a, b, c and line1 \ r, line2, line3 \ r. For these inputs, Split returns 3 lines or 4 lines, respectively.

+5
May 22 '14 at 3:35
source share

If you want to use StreamReader, then yes, you will need to use ReadLine and a loop through StreamReader, counting line by line.

Something like that:

 string line; using (StreamReader reader = new StreamReader(stream)) { while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } 

or try

 using (StreamReader reader = new StreamReader("file.txt")) { string[] content = reader.ReadToEnd().Replace("\n","").Split('\t'); } 
0
Nov 09 '12 at 17:29
source share

Using the following extension method:

 public static class Extensions { public static IEnumerable<string> ReadAllLines(this StreamReader reader) { string line; while ((line = reader.ReadLine()) != null) { yield return line; } } } 

You can get the code you need:

 using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Test_Resources.Resources.Accounts.txt")) using (StreamReader reader = new StreamReader(stream)) { string[] result = reader.ReadAllLines().ToArray(); } 
0
Dec 22 '18 at 15:41
source share



All Articles