Streamreader C# Start Read at Line Number
C# StreamReader is used to read characters to a stream in a specified encoding. StreamReader.Read method reads the next character or next set of characters from the input stream. StreamReader is inherited from TextReader that provides methods to read a graphic symbol, block, line, or all content.
StreamReader is divers in the Organization.IO namespace. StreamReader provides the following methods:
- Peak – Returns if there is a grapheme or not.
- Read - Reads the next character or side by side set of characters from the input stream.
- ReadAsync - Reads a specified maximum number of characters from the current stream asynchronously and writes the information to a buffer, beginning at the specified index.
- ReadBlock - Reads a specified maximum number of characters from the current stream and writes the information to a buffer, offset at the specified alphabetize.
- ReadBlockAsync - Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, start at the specified alphabetize.
- ReadLine - Reads a line of characters from the current stream and returns the data as a string.
- ReadLineAsync - Reads a line of characters asynchronously from the current stream and returns the data as a string.
- ReadToEnd - Reads all characters from the current position to the finish of the stream.
- ReadToEndAsync - Reads all characters from the current position to the end of the stream asynchronously and returns them as i cord.
Creating a StreamReader from a Filename
StreamReader constructor takes a full file name or a FileStream object with Encoding, and buffer size as optional parameters.
The following code snippet creates a StreamReader from a filename with default encoding and buffer size.
- // File proper name
- cord fileName = @"C:\Temp\CSharpAuthors.txt" ;
- StreamReader reader = new StreamReader(fileName)
The following lawmaking example creates a StreamReader and reads a file content 1 line at a time and displays to the console. If at that place is an exception, the exception is displayed on the console.
- using Arrangement;
- using System.IO;
- using Arrangement.Text;
- class Plan
- {
- static void Main(cord[] args)
- {
- // Fileproper noun
- string fileName = @"C:\Temp\CSharpAuthors.txt" ;
- try
- {
- //Create a StreamReader
- using (StreamReader reader = new StreamReader(fileName))
- {
- string line;
- //Read line by line
- while ((line = reader.ReadLine()) !=null )
- {
- Console.WriteLine(line);
- }
- }
- }
- catch (Exception exp)
- {
- Console.WriteLine(exp.Message);
- }
- Console.ReadKey();
- }
- }
In the in a higher place example, the file name is the complete path including a file on a Universal Naming Convention (UNC) share. The default encoding is UTF8Encoding and the buffer size to 1024 bytes.
Creating a StreamReader using FileInfo.OpenText
StreamReader tin can be created using the FileInfo.OpenText() method. The following code snippet creates a StreamReader using the FileInfo.OpenText() method.
- FileInfo fi = new FileInfo(fileName);
- StreamReader sr = fi.OpenText();
- string southward = "" ;
- while ((s = sr.ReadLine()) != nil ) {
- Console.WriteLine(s);
- }
FileInfo grade is useful with file operations. Learn more near FileInfo course here: FileInfo in C#
Creating a StreamReader using a FileStream
StreamReader constructor takes a FileStream object with Encoding, and buffer size as optional parameters. The post-obit code snippet creates a StreamReader from a FileStream with default encoding and buffer size.
- // Create a StreamReader from a FileStream
- using (StreamReader reader = new StreamReader( new FileStream(fileName, FileMode.Open)))
- {
- string line;
- while ((line = reader.ReadLine()) != null )
- {
- Console.WriteLine(line);
- }
- }
The post-obit lawmaking snippet creates a StreamReader from a FileStream with Encoding. Encoding supported are ASCII, Unicode, UTF32, UTF7, and UTF8. Encoding is defined in the System.Text namespace.
- using (StreamReader reader = new StreamReader(
- new FileStream(fileName, FileMode.Open), Encoding.UTF8))
The following code snippet creates a StreamReader from a FileStream with encoding and buffer size.
- using (StreamReader reader = new StreamReader(
- new FileStream(fileName, FileMode.Open), Encoding.UTF8, truthful , 1024))
StreamReader.Height() method
Stream.Reader.Peek() method checks if there are more characters are left to read in a file. The method returns an integer. If the value is less than 0, at that place are no more characters left to read. The following code snippet uses the Peak() method to bank check if there are more characters left to read in a file.
- using (StreamReader reader = new StreamReader( new FileStream(fileName, FileMode.Open up)))
- {
- while (reader.Peek() >= 0)
- {
- }
- }
StreamReader.ReadBlock() method
StreamReader.Read() method reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index. The position of the underlying stream is advanced past the number of characters that were read into buffer. The method blocks until either count characters are read, or the end of the stream has been reached. This is a blocking version of Read.
StreamReader.ReadBlockAsync() method reads a specified maximum number of characters from the electric current stream asynchronously and writes the data to a buffer, kickoff at the specified index.
StreamReader.Read() method
StreamReader.Read() method reads the next char or next set of chars from the input stream. StreamReader.ReadAsync() method reads the next char or next set of chars from the input stream asynchronously.
The post-obit code snippet reads a stream using the Read() method.
- using (StreamReader reader = new StreamReader( new FileStream(fileName, FileMode.Open)))
- {
- while (reader.Peek() >= 0)
- {
- Console.WriteLine((char )reader.Read());
- }
- }
StreamReader.ReadLine() method
StreamReader.ReadLine() method reads a line of characters from the electric current stream and returns the information as a string.
StreamReader.ReadLineAsync() method reads a line of characters from the current stream asynchronously and returns the data equally a string.
The following lawmaking snippet reads a file using the ReadLine() method.
- using (StreamReader reader = new StreamReader( new FileStream(fileName, FileMode.Open)))
- {
- string line;
- while ((line = reader.ReadLine()) != nada )
- {
- Console.WriteLine(line);
- }
- }
StreamReader.ReadToEnd() method
StreamReader.ReadToEnd() method reads all characters from the current position to the end of the stream.
StreamReader.ReadToEndAsync() method reads all characters from the electric current position to the stop of the stream.
The following code snippet reads entire file using the ReadToEnd() method.
- using (StreamReader reader = new StreamReader(fileName))
- {
- Console.WriteLine(reader.ReadToEnd());
- }
StreamReader Lawmaking Example
Here is the consummate code example of utilise of StreamReader and its methods. The code too reads the content of the file and displays on the console.
- using System;
- using System.IO;
- using System.Text;
- class Programme
- {
- static void Main( cord [] args)
- {
- string fileName = @ "C:\Temp\CSharpAuthors.txt" ;
- try
- {
- using (StreamReader reader = new StreamReader(fileName))
- {
- Console.WriteLine(reader.ReadToEnd());
- }
- catch (Exception exp)
- {
- Console.WriteLine(exp.Message);
- }
- Console.ReadKey();
- }
- }
The output of the above lawmaking looks like the post-obit:
Summary
In this commodity and lawmaking sample, we saw how to utilise a StreamReader course to read a text file.
Source: https://www.c-sharpcorner.com/article/working-with-c-sharp-streamreader/
0 Response to "Streamreader C# Start Read at Line Number"
Post a Comment