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
31
votes
5 answers

How to skip first line while reading csv using streamreader

I have my following code to read values from CSV file and do some processing. I would like to skip the first row of the input CSV file as it contains header text but I'd want to add it back after the processing is done. List values = new…
Ye Myat Aung
  • 1,783
  • 11
  • 31
  • 49
30
votes
6 answers

How to count lines fast?

I tried unxutils' wc -l but it crashed for 1GB files. I tried this C# code long count = 0; using (StreamReader r = new StreamReader(f)) { string line; while ((line = r.ReadLine()) != null) { count++; } } return count; It…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
30
votes
4 answers

Load a simple text file in Android Studio

Got a brand new project using Google's new Android Studio IDE. I'm trying to load a simple text file using an InputStreamReader. I'm getting a file not found exception. Now there isn't any assets/ folder. I tried to create one and add my file at…
Yohann T.
  • 1,915
  • 2
  • 23
  • 28
28
votes
3 answers

How do I convert StreamReader to a string?

I altered my code so I could open a file as read only. Now I am having trouble using File.WriteAllText because my FileStream and StreamReader are not converted to a string. This is my code: static void Main(string[] args) { string inputPath =…
Jake H.
  • 563
  • 2
  • 11
  • 23
28
votes
4 answers

Reading a line from a streamreader without consuming?

Is there a way to read ahead one line to test if the next line contains specific tag data? I'm dealing with a format that has a start tag but no end tag. I would like to read a line add it to a structure then test the line below to make sure it not…
Crash893
  • 11,428
  • 21
  • 88
  • 123
27
votes
5 answers

How can I tell when I've reached the end of the file when using the ReadBlock method in C#?

I noticed that it will keep returning the same read characters over and over, but I was wondering if there was a more elegant way.
abw333
  • 5,571
  • 12
  • 41
  • 48
27
votes
7 answers

Getting path to the parent folder of the solution file using C#

I am a beginner in C#, and I have a folder from which I am reading a file. I want to read a file which is located at the parent folder of the solution file. How do I do this? string path = ""; StreamReader sr = new StreamReader(path); So if my file…
User1204501
  • 781
  • 3
  • 14
  • 26
24
votes
4 answers

Read text file from C# Resources

I need to read a file from my resources and add it to a list. my code: private void Form1_Load(object sender, EventArgs e) { using (StreamReader r = new…
Dobz
  • 1,213
  • 1
  • 14
  • 36
22
votes
6 answers

c# - StreamReader and seeking

Can you use StreamReader to read a normal textfile and then in the middle of reading close the StreamReader after saving the current position and then open StreamReader again and start reading from that poistion ? If not what else can I use to…
Stacker
  • 8,157
  • 18
  • 73
  • 135
22
votes
3 answers

How can I detect if a .NET StreamReader found a UTF8 BOM on the underlying stream?

I get a FileStream(filename,FileMode.Open,FileAccess.Read,FileShare.ReadWrite) and then a StreamReader(stream,true). Is there a way I can check if the stream started with a UTF8 BOM? I am noticing that files without the BOM are read as UTF8 by the…
bookclub
  • 579
  • 2
  • 4
  • 8
21
votes
4 answers

C# StreamReader, "ReadLine" For Custom Delimiters

What is the best way to have the functionality of the StreamReader.ReadLine() method, but with custom (String) delimiters? I'd like to do something like: String text; while((text = myStreamReader.ReadUntil("my_delim")) != null) { …
Eric
  • 2,098
  • 4
  • 30
  • 44
21
votes
9 answers

HTTPWebResponse + StreamReader Very Slow

I'm trying to implement a limited web crawler in C# (for a few hundred sites only) using HttpWebResponse.GetResponse() and Streamreader.ReadToEnd() , also tried using StreamReader.Read() and a loop to build my HTML string. I'm only downloading pages…
Roey
  • 849
  • 2
  • 11
  • 20
21
votes
10 answers

.NET C# - Random access in text files - no easy way?

I've got a text file that contains several 'records' inside of it. Each record contains a name and a collection of numbers as data. I'm trying to build a class that will read through the file, present only the names of all the records, and then…
tabull
21
votes
4 answers

Tracking the position of the line of a streamreader

I need to track the position of the line that I am reading from the stream reader. When I say reader.ReadLine(), I need to know the position of that line in the file and I also want to be able to then read the file from the position I have…
johnnie
  • 1,837
  • 5
  • 23
  • 36
19
votes
4 answers

How to skip invalid characters in stream in Java/Scala?

For example I have following code Source.fromFile(new File( path), "UTF-8").getLines() and it throws exception Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1 at…
yura
  • 14,489
  • 21
  • 77
  • 126