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:

  1. Peak – Returns if there is a grapheme or not.
  2. Read - Reads the next character or side by side set of characters from the input stream.
  3. 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.
  4. ReadBlock - Reads a specified maximum number of characters from the current stream and writes the information to a buffer, offset at the specified alphabetize.
  5. 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.
  6. ReadLine - Reads a line of characters from the current stream and returns the data as a string.
  7. ReadLineAsync - Reads a line of characters asynchronously from the current stream and returns the data as a string.
  8. ReadToEnd - Reads all characters from the current position to the finish of the stream.
  9. 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.

  1. // File proper name
  2. cord fileName = @"C:\Temp\CSharpAuthors.txt" ;
  3. 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.

  1. using Arrangement;
  2. using System.IO;
  3. using Arrangement.Text;
  4. class Plan
  5. {
  6. static  void Main(cord[] args)
  7. {
  8. // Fileproper noun
  9. string fileName = @"C:\Temp\CSharpAuthors.txt" ;
  10. try
  11. {
  12. //Create  a StreamReader
  13. using (StreamReader reader = new StreamReader(fileName))
  14. {
  15. string line;
  16. //Read  line by  line
  17. while ((line = reader.ReadLine()) !=null )
  18. {
  19. Console.WriteLine(line);
  20. }
  21. }
  22. }
  23. catch (Exception exp)
  24. {
  25. Console.WriteLine(exp.Message);
  26. }
  27. Console.ReadKey();
  28. }
  29. }

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.

  1. FileInfo fi = new  FileInfo(fileName);
  2. StreamReader sr = fi.OpenText();
  3. string  southward = "" ;
  4. while  ((s = sr.ReadLine()) != nil ) {
  5. Console.WriteLine(s);
  6. }

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.

  1. // Create a StreamReader from a FileStream
  2. using  (StreamReader reader = new  StreamReader( new  FileStream(fileName, FileMode.Open)))
  3. {
  4. string  line;
  5. while  ((line = reader.ReadLine()) != null )
  6. {
  7. Console.WriteLine(line);
  8. }
  9. }

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.

  1. using  (StreamReader reader = new  StreamReader(
  2. new  FileStream(fileName, FileMode.Open), Encoding.UTF8))

The following code snippet creates a StreamReader from a FileStream with encoding and buffer size.

  1. using  (StreamReader reader = new  StreamReader(
  2. 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.

  1. using  (StreamReader reader = new  StreamReader( new  FileStream(fileName, FileMode.Open up)))
  2. {
  3. while  (reader.Peek() >= 0)
  4. {
  5. }
  6. }

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.

  1. using  (StreamReader reader = new  StreamReader( new  FileStream(fileName, FileMode.Open)))
  2. {
  3. while  (reader.Peek() >= 0)
  4. {
  5. Console.WriteLine((char )reader.Read());
  6. }
  7. }

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.

  1. using  (StreamReader reader = new  StreamReader( new  FileStream(fileName, FileMode.Open)))
  2. {
  3. string  line;
  4. while  ((line = reader.ReadLine()) != nada )
  5. {
  6. Console.WriteLine(line);
  7. }
  8. }

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.

  1. using  (StreamReader reader = new  StreamReader(fileName))
  2. {
  3. Console.WriteLine(reader.ReadToEnd());
  4. }

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.

  1. using  System;
  2. using  System.IO;
  3. using  System.Text;
  4. class  Programme
  5. {
  6. static void  Main( cord [] args)
  7. {
  8. string  fileName = @ "C:\Temp\CSharpAuthors.txt" ;
  9. try
  10. {
  11. using  (StreamReader reader = new  StreamReader(fileName))
  12. {
  13. Console.WriteLine(reader.ReadToEnd());
  14. }
  15. catch  (Exception exp)
  16. {
  17. Console.WriteLine(exp.Message);
  18. }
  19. Console.ReadKey();
  20. }
  21. }

The output of the above lawmaking looks like the post-obit:

C# StreamReader

Summary

In this commodity and lawmaking sample, we saw how to utilise a StreamReader course to read a text file.

morganallon1966.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel