Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use StreamReader and StreamWriter classes to read and write operating files in C #

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "C # how to use StreamReader and StreamWriter class read and write operation files", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "C # how to use StreamReader and StreamWriter class read and write operation files" bar!

1. Text reading and writing class:

TextReader/TextWriter: text reading and writing, abstract classes

1. TextReader text reading, which derives from:

StreamReader: reads characters from a byte stream in a specific encoding.

StringReader: read from a string.

2. TextWriter text writing, which derives from:

StreamWriter: writes characters to the stream in a specific encoding.

StringWriter: writes information to a string, which is stored in the underlying StringBuilder.

IndentedTextWriter: provides a text writer that indents new lines based on Tab string tags.

HttpWriter: provides HttpResponse objects that are accessed through internal TextWriter objects.

HtmlTextWriter: writes markup characters and text to the ASP.NET server control output stream. This class provides formatting capabilities that the ASP.NET server control uses when rendering markup to the client.

Second, StreamReader class, read file 1, example:

Constructor: default encoding is UTF-8

StreamReader srAsciiFromFile = new StreamReader ("C:\\ Temp\\ Test.txt", System.Text.Encoding.ASCII); StreamReader srAsciiFromStream = new StreamReader ((System.IO.Stream) File.OpenRead ("C:\\ Temp\\ Test.txt"), System.Text.Encoding.ASCII); 1. Read the text Read () from the file, Peek () using (StreamReader sr = new StreamReader (path)) {while (sr.Peek () > = 0) {Console.Write ((char) sr.Read ()) 2. Call its ReadAsync () method to read the file asynchronously. Static async Task Main () {await ReadAndDisplayFilesAsync ();} static async Task ReadAndDisplayFilesAsync () {String filename = "C:\\ s.xml"; Char [] buffer; using (var sr = new StreamReader (filename)) {buffer = new Char [(int) sr.BaseStream.Length]; await sr.ReadAsync (buffer, 0, (int) sr.BaseStream.Length);} Console.WriteLine (new String (buffer));} 3, read a line of characters. ReadLine () using (StreamReader sr = new StreamReader ("TestFile.txt")) {string line; / / Read and display lines from the file until the end of the file is reached. While ((line = sr.ReadLine ())! = null) {Console.WriteLine (line);}} 4, read to the end of a file in operation. ReadToEnd () using (StreamReader sr = new StreamReader (path)) {Console.WriteLine (sr.ReadToEnd ());} III. StreamWriter class, write file examples:

The StreamWriter class allows characters and strings to be written directly to the file

/ / keep the existing data in the file, open the d:\ file.txt file using (StreamWriter sw = new StreamWriter (@ "d:\ file.txt", true)) / / true means to append {/ / write a new string to the file and close StreamWriter sw.WriteLine ("Another File Operation Method") } Thank you for your reading, the above is the content of "how to use StreamReader and StreamWriter classes to read and write files". After the study of this article, I believe you have a deeper understanding of how to use StreamReader and StreamWriter classes to read and write files, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report