TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# TextReader, Returned by File.OpenText

Use the TextReader class. TextReader is returned by File.OpenText.
TextReader reads text characters from a file. It is internally used by many other types in the .NET Framework. But you can access it for a general method of reading text content. It's usually better to use StreamReader in your programs.TextWriter
Example. Initially here we look at an example of using the TextReader class. We use the using statement with resource acquisition statements, providing for proper cleanup of the system resources related to the operating system and file system.Using

OpenText: The TextReader is returned by File.OpenText. This is a common usage of TextReader.

File

ReadLine: This method internally scans the file for the next newline sequence, and then consumes the text up to that point and returns it.

Environment.NewLine

ReadBlock: This method accepts a pre-allocated char[] array and the start index and count parameters. It fills the array elements before returning.

Char Array

Peek: This method physically examines the file contents, and then returns the very next character that would be read.

C# program that uses TextReader class using System; using System.IO; class Program { static void Main() { // // Read one line with TextReader. // using (TextReader reader = File.OpenText(@"C:\perl.txt")) { string line = reader.ReadLine(); Console.WriteLine(line); } // // Read three text characters with TextReader. // using (TextReader reader = File.OpenText(@"C:\perl.txt")) { char[] block = new char[3]; reader.ReadBlock(block, 0, 3); Console.WriteLine(block); } // // Read entire text file with TextReader. // using (TextReader reader = File.OpenText(@"C:\perl.txt")) { string text = reader.ReadToEnd(); Console.WriteLine(text.Length); } // // Peek at first character in file with TextReader. // using (TextReader reader = File.OpenText(@"C:\perl.txt")) { char first = (char)reader.Peek(); Console.WriteLine(first); } } } Output First line Fir 18 F
TextWriter. The TextWriter class also is an abstract base class that provides functionality for the StreamWriter class. The StreamWriter class implements additional logic for text encodings, making it a more useful class in many cases.
StreamReader. The StreamReader class is derived from the TextReader class. The StreamReader class contains additional logic for reading text files with various encodings more correctly. For this reason, StreamReader is usually better for simple uses.StreamReader

But: If you are trying to create your own reader object, using an instance field that is a TextReader is possibly preferable.

Summary. We explored the TextReader class in the C# programming language targeting the .NET Framework. We can use the using statement with a resource acquisition statement, and then read lines, blocks of text, or entire files into strings.

Review: The Peek method also allows you to preview what the TextReader will read next and where its reading position is.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf