Questions tagged [streamreader]

StreamReader is a C# class designed for character input in a particular encoding, it can be used for reading lines of information from a standard text file. By default, a StreamReader is not thread safe.

Implements a TextReader that reads characters from a byte stream in a particular encoding.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. StreamReader can be used for reading lines of information from a standard text file.

StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system. If you get the current character encoding using the CurrentEncoding property, the value is not reliable until after the first Read method, since encoding auto detection is not done until the first call to a Read method.

By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.

Official Microsoft documentation

1931 questions
6
votes
1 answer

reading and modifying large text files 3-5GB

I have a rather large file consisting of several million lines and there is the need to check and remove corrupt lines from the file. I have shamelessly tried File.ReadAllLines but it didn't work. Then I tried to stream lines as below reading from…
mechanicum
  • 699
  • 3
  • 14
  • 25
6
votes
2 answers

How to read byte[] with current encoding using streamreader

I would like to read byte[] using C# with the current encoding of the file. As written in MSDN the default encoding will be UTF-8 when the constructor has no encoding: var reader = new StreamReader(new MemoryStream(data)). I have also tried this,…
Ori
  • 115
  • 1
  • 2
  • 14
6
votes
3 answers

Reading text from Amazon s3 stream

I use the following code to read text file from Amazon S3, and processing it line by line. This code works but the problem is it is slow. GetObjectRequest getObjRequest = new GetObjectRequest() .WithBucketName(amazonSettings.BucketName) …
Raed Alsaleh
  • 1,581
  • 9
  • 27
  • 50
6
votes
3 answers

Long time to load first connection in C# .NET

I'm making a program that connects to a website and downloads XML from it. It then displays the information to the user. The problem I am having is when I first open the program and start downloading the XML information it takes a really long time.…
Christian
  • 135
  • 2
  • 8
6
votes
3 answers

What character encoding is used by StreamReader.ReadToEnd()?

What character encoding is used by StreamReader.ReadToEnd()? What would be the reason to use (b) instead of (a) below? Is there a risk of their being a character encoding problem if (a) is used instead of (b)? Is there another method that is…
CJ7
  • 22,579
  • 65
  • 193
  • 321
6
votes
6 answers

Memory Leak(?) with StreamReader

I have a few very large files each of 500MB++ size, containing integer values (in fact it's a bit more complex), I'm reading those files in a loop and calculating the max value for all files. By some reason the memory is growing constantly during…
user1514042
  • 1,899
  • 7
  • 31
  • 57
6
votes
2 answers

How to open a StreamReader in ShareDenyWrite mode?

How do i open a StreamReader with FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE? Same question, slightly expanded How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can read the…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
5
votes
2 answers

converting \u0040 to @ in C#

The Facebook graph API's return to me the user's email address as foo\u0040bar.com. in a JSON object. I need to convert it to foo@bar.com. There must be a built in method in .NET that changes the Unicode character expression (\u1234) to the…
Barka
  • 8,764
  • 15
  • 64
  • 91
5
votes
9 answers

C# add line numbers to a text file

I am trying to read a text file in C# and add line numbers to the lines. This my input file: This is line one this is line two this is line three And this should be the output: 1 This is line one 2 this is line two 3 this is…
Vojtech
  • 643
  • 3
  • 16
  • 30
5
votes
1 answer

Specified method is not supported while using SeekOrigin.Begin on stream

I have a StreamReader which I am using to deserialize my request later on as shown below - Here s is Stream object - using (var reader = new StreamReader(s)) { using (var jsonReader = new JsonTextReader(reader)) { var ser = new…
AndyP
  • 527
  • 1
  • 14
  • 36
5
votes
2 answers

C# Read webpage content Streamreader

I need to read the content of a webpage in streamreader like www.example.com i got this: System.IO.StreamReader StreamReader1 = new System.IO.StreamReader("www.example.com"); string test =…
Lars Werkman
  • 2,528
  • 2
  • 20
  • 20
5
votes
3 answers

Strange question mark, when setting StreamReader to beginning

I am writing a program about job interview. Everything is working properly, except one thing. When I use an outside method TotalLines (where I have seperate StreamReader), it is working properly, but when I am calculating a number of totalLines in…
5
votes
9 answers

C# "using" blocks

I've got something like the code below...someone here mentioned that the WebClient, Stream, and StreamReader objects could all benefit from using blocks. Two easy questions: 1: How would this little snippet look with using blocks? I've no problem…
J Benjamin
  • 4,722
  • 6
  • 29
  • 39
5
votes
1 answer

Powershell StandardOutput buffer too small for external command

Some useful info beforehand. What I'm attempting to do is read in output from an external command, specifically steamcmd, using powershell start-process and System.diagnostics.ProcessStartInfo. What I'm running into is RedirectStandardOutput buffer…
5
votes
1 answer

C# - Setting XML Node values as Stings from StreamReader result

I'm using an API call to return some XML data from a web server. The XML data is in the following format: The time you think you're missing, misses you too.
Rawns
  • 865
  • 4
  • 27