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# TextWriter, Returned by File.CreateText

Use the TextWriter class. TextWriter is returned by File.CreateText.
TextWriter creates text output. It is used to implement other file classes in the base class library. You can also use it to implement lower-level file IO logic in your higher-level classes.TextReader
Example. First here we look at the TextWriter class, which we instantiate from the result of the File.CreateText static method in the System.IO namespace. The TextWriter class provides useful methods for writing lines and text to a file.File

Using: The TextWriter class is easiest to use reliably when you wrap it in a using statement and a resource acquisition statement.

Using

WriteLine: The program uses the WriteLine method and the Write method on the TextWriter instance we allocated.

Write: This method does not add the newline sequence. It just writes the character data specified.

NewLine: The program uses the NewLine property. By default, it will be set to the platform's default newline sequence, which on Windows is "\r\n".

Environment.NewLine
C# program that uses TextWriter using System.IO; class Program { static void Main() { // // Create a new TextWriter in the resource acquisition statement. // using (TextWriter writer = File.CreateText("C:\\perl.txt")) { // // Write one line. // writer.WriteLine("First line"); // // Write two strings. // writer.Write("A "); writer.Write("B "); // // Write the default newline. // writer.Write(writer.NewLine); } } } File created by program First line A B
StreamWriter. The StreamWriter class actually inherits from the TextWriter class in the base class library. The TextWriter class is an abstract base class, which means it provides functionality for StreamWriter.

Note: Microsoft states that StreamWriter represents writing in a particular encoding.

StreamWriter

And: For this reason, unless you have specialized encoding requirements, StreamWriter is more appropriate because it manages the encoding.

Summary. We looked at the TextWriter class and related it to the StreamWriter class by noting that it is used as a base class. The TextWriter class is lower-level than StreamWriter because it does not handle encodings automatically.

But: It is still useful in many cases when you have specific encoding requirements or always want to use ASCII encoding.

© 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