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
9
votes
4 answers

How to limit the number of characters read by StreamReader.ReadLine() in .NET?

I am writing a web server application in C# and using StreamReader class to read from an underlying NetworkStream: NetworkStream ns = new NetworkStream(clientSocket); StreamReader sr = new StreamReader(ns); String request = sr.ReadLine(); This…
Roman Shumikhin
  • 613
  • 2
  • 6
  • 10
9
votes
2 answers

Reading and writing very large text files in C#

I have a very large file, almost 2GB in size. I am trying to write a process to read the file in and write it out without the first row. I pretty much have been only able to read and write one line at a time which takes forever. I can open it,…
Cass
  • 537
  • 1
  • 7
  • 24
9
votes
7 answers

Will closing a FileStream close the StreamReader?

If I use a FileStream to create a StreamReader, will the StreamReader close when I close the FileStream or will I need to close the StreamReader too? public void ReadFile() { var file = new FileStream("c:\file.txt", FileMode.Open,…
Nick O
  • 3,716
  • 6
  • 38
  • 50
9
votes
1 answer

c# MemoryStream Encoding Vs. Encoding.GetChars()

I am trying to copy a byte stream from a database, encode it and finally display it on a web page. However, I am noticing different behavior encoding the content in different ways (note: I am using the "Western European" encoding which has a Latin…
Sidawy
  • 574
  • 1
  • 3
  • 17
8
votes
5 answers

Need to pick up line terminators with StreamReader.ReadLine()

I wrote a C# program to read an Excel .xls/.xlsx file and output to CSV and Unicode text. I wrote a separate program to remove blank records. This is accomplished by reading each line with StreamReader.ReadLine(), and then going character by…
Tony Trozzo
  • 1,231
  • 6
  • 20
  • 34
8
votes
2 answers

StreamReader and buffer in C#

I've a question about buffer usage with StreamReader. Here: http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx you can see: "When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal…
gravitar
  • 81
  • 1
  • 1
  • 2
8
votes
1 answer

"stream was not readable" ArgumentException when using ReadAsStreamAsync with StreamReader

I have the following piece of code to read and process the response of a Http get request using StreamReader: try { Stream stream = await ExecuteRequestAsync(uriBuilder.Uri, HttpMethod.Get).ConfigureAwait(false); if (stream != null) { …
Romonov
  • 8,145
  • 14
  • 43
  • 55
8
votes
8 answers

check if string exists in a file

I have the following piece of code which opens a text file and reads all the lines in the file and storing it into a string array. Which then checks if the string is present in the array. However the issue I'm facing is that whenever a string is…
BryanZest
  • 83
  • 1
  • 1
  • 4
8
votes
2 answers

C++ event driven json stream reader

I just discovered YAJL project which just does what I need. Read from stream Callback on each valid parsed token Reparse incomplete json when new data arrived But I prefer C++. Of course I can use this library from C++ project and even write my…
Sergey
  • 323
  • 4
  • 10
7
votes
5 answers

C# read line from file with StreamReader with DownloadFileAsync

I am having a problem reading file with StreamReader and while line != null add to textBox1 Code: using(StreamReader reader = new StreamReader("lastupdate.txt")) { string line; while((line = reader.ReadLine()) != null) { …
user1085907
  • 1,009
  • 2
  • 16
  • 40
7
votes
3 answers

C# - FileStream: both lock a file and at the same time be able to read it without truncating it and write it with truncating it

I suppose my title isn't that clear. I'll try to explain: I can write and read a file using a FileStream FileStream fs = new FileStream("C:\\Users\\Public\\text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); private void…
pikachu
  • 1,031
  • 2
  • 11
  • 20
7
votes
3 answers

What is the encoding of the string get from StreamReader.ReadLine()

First, let's see the code: //The encoding of utf8.txt is UTF-8 StreamReader reader = new StreamReader(@"C:\\utf8.txt", Encoding.UTF8, true); while (reader.Peek() > 0) { //What is the encoding of lineFromTxtFile? string lineFromTxtFile =…
jjooeell
  • 113
  • 1
  • 7
7
votes
2 answers

How can I know if a text file ends with carriage return or not?

I have to process a text file and check if it ends with a carriage return or not. I have to read to whole content, make some changes and re-write it into the target file, keeping exactly the same formatting as original. And here is the problem: I…
7
votes
2 answers

Do I need to Dispose XmlReader if I Dispose its underlying Stream?

I have the following method GetData that creates a StreamReader from a file. private void GetData(string file) { string filename = Path.GetFileNameWithoutExtension(file); XmlDocument xmldoc = new XmlDocument(); using (StreamReader sr =…
Leonard Thieu
  • 785
  • 1
  • 7
  • 21
7
votes
5 answers

How to create copy of file using StreamReader and StreamWriter

I need to use StreamReader to read a .txt file on a console application, then create a new file or backup with a different name but same content. The problem is i cant figure out how to use the content from the first file to place into the new one.…
claraichu
  • 71
  • 1
  • 1
  • 2