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
4
votes
1 answer

How to: Save a StreamReader content in a string

How to: Save a StreamReader content in a string I am trying to save a StreamReader content in a string. Unfortunately, I am not allowed to save the content, because the object seems to be lost (coming from a FTP server). Error message GERMAN: Auf…
sjantke
  • 605
  • 4
  • 9
  • 35
4
votes
1 answer

ASP.NET MVC: Can an MSI file be returned via a FileContentResult without breaking the install package?

I'm using this code to return a FileContentResult with an MSI file for the user to download in my ASP.NET MVC controller: using (StreamReader reader = new StreamReader(@"c:\WixTest.msi")) { Byte[] bytes =…
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137
4
votes
5 answers

Text written to Console after reading from .txt file using streamreader only partially appearing

I'm trying to search for a customer account stored in a text file by the town they live in. There are three customer accounts all with different towns. I think the program finds the correct customer as it does returns data to write to the screen but…
COYG
  • 1,538
  • 1
  • 16
  • 31
4
votes
2 answers

StreamWriter Multi Threading C#

I would like to ask help on my code. I am a newbie and wanted to implement safe multi threading in writing to a text file. StreamWriter sw = new StreamWriter(@"C:\DailyLog.txt"); private void Update(){ var collection =…
Wylan Osorio
  • 1,136
  • 5
  • 19
  • 46
4
votes
3 answers

StreamReader does not read in carriage returns

I have to write a console application for a computer course that I'm taking. The program reads text in from a file using StreamReader, splits the string into single words and saves them in a String array and then prints the words out…
John McDonald
  • 69
  • 1
  • 10
4
votes
1 answer

System.IO.IOException: The process cannot access the file because it is being used by another process

I'm using a FileSystemWatcher to monitor a log file for changes. The log file is written by a 3rd party application. Once a change is triggered, I am attempting to read the file using: using (FileStream fs = new FileStream(e.FullPath, FileMode.Open,…
CathalMF
  • 9,705
  • 6
  • 70
  • 106
4
votes
3 answers

Reading file after writing it

I have a strange problem. So my code follows as following. The exe takes some data from the user Call a web service to write(and create CSV for the data) the file at perticular network location(say \some-server\some-directory). Although this web…
Anand
  • 14,545
  • 8
  • 32
  • 44
4
votes
3 answers

Java make a copy of a reader

I have a BufferedReader looping through a file. When I hit a specific case, I would like to continue looping using a different instance of the reader but starting at this point. Any ideas for a recommended solution? Create a separate reader, use…
tinkertime
  • 2,972
  • 4
  • 30
  • 45
4
votes
2 answers

StreamReader, C#, peek

I have a StreamReader that once in a while check if it has more to read from a simple text file. It uses peek property. The problem is that when I am using peek the position is changed, althougth not suppose to. FileStream m_fsReader = new…
Boris Raznikov
  • 2,373
  • 10
  • 34
  • 56
4
votes
1 answer

Reading very large text files, should I be incorporating async?

I have been challenged with producing a method that will read in very large text files into a program these files can range from 2gb to 100gb. The idea so far has been to read say a couple of 1000 lines of text into the method. At the moment the…
user2169674
  • 87
  • 2
  • 8
4
votes
1 answer

How to read mixed file of byte and string

I've a mixed file with a lot of string line and part of byte encoded data. Example: --Begin Attach Content-Info: /Format=TIF Content-Description: 30085949.tif (TIF File) Content-Transfer-Encoding: binary; Length=220096 II*II* Îh …
davymartu
  • 1,393
  • 3
  • 15
  • 34
4
votes
3 answers

Parsing a text file using C# StreamReader

I have a text file that contains some hymns in a particular format.Example below. 1 Praise to the Lord 1 Praise to the Lord, the Almighty, the King of creation! O my soul, praise Him, for He is thy health and salvation! All ye who hear, now…
Joel Dean
  • 2,444
  • 5
  • 32
  • 50
4
votes
2 answers

Use stream reader for a string variable

Is it possible to make the stream reader to read a string variable, or a textbox instead of a text file? If yes, how?
user2023328
  • 125
  • 2
  • 11
4
votes
2 answers

Java-Sockets: InputStream.read() vs BufferedReader.read()

I'm reading from a Socket's InputStream. Because I'm parsing the incoming data on the fly, I'm required to read character by character. Does BufferedReader.read() the same thing as InputStream.read() does ? (assuming that BufferedReader has been…
Atmocreations
  • 9,923
  • 15
  • 67
  • 102
4
votes
2 answers

Using StreamReader.ReadLine in loop only reads one line from text file

see comments for resolution--file was in wrong place I've searched for an answer all over the place, but I haven't been able to find one. This is really frustrating to me because I've never had this much trouble reading from a file with any other…
Steve Schmith
  • 43
  • 1
  • 1
  • 4