StreamReader/StreamWriter in C #

Source: Internet
Author: User

In the process of reading and writing files, filestream can only perform read and write operations on the original data stream in bytes. Therefore, C # provides more powerful streamreader/streamwriter to support reading and writing files. streamreader/streamwriter can read and write data in characters ..

I. constructor using streamreader

Streamreader has many constructor functions: only two common and common constructor functions are listed here.

Streamreader sr = new streamreader (Stream); stream can be filestream;

Streamreader sr = new streamreader (string); string initializes a new instance of the streamreader class for the specified file name.

For example:

// Demo1

Filestream filest = new filestream (@ "C: \ abc.txt", filemode. Open, fileaccess. readwrite); streamreader sr = new streamreader (filest );

// Demo2

Streamreader another = new streamreader (@ "C: \ abc.txt ");

Both streamreader point to the same file.

Code for streamreader to read data in a file:

FileStream filest = new FileStream (@ "c: \ abc.txt", FileMode. Open, FileAccess. ReadWrite );

StreamReader sr = new StreamReader (filest );

String strLine = sr. ReadLine (); // read a row in the file

While (strLine! = Null) // determines whether it is null, indicating that the last row of the file is reached.

{

Console. WriteLine (strLine );

StrLine = sr. ReadLine ();

}

Sr. Close (); // Close the stream

Filest. Close ();

The content in the program running result and the abc.txt file in the disk is compared as follows:

   

Ii. StreamWriter

StreamWriter is used to write data to files. It is similar to StreamReader. It is only responsible for writing data to files and reading data from files.

StreamWriter constructor also has many constructor types. Here, we only use two constructor types.

StreamWriter sr = new StreamWriter (Stream); Stream can be Filestream;

StreamWriter sr = new StreamWriter (String); String initializes a new instance of the StreamWriter class for the specified file name.

For example:

// Demo1

FileStream filest = new FileStream (@ "c: \ abc.txt", FileMode. Open, FileAccess. ReadWrite); StreamWriter sw = new StreamWriter (filest );

// Demo2

StreamWriter another = new StreamWriter (@ "c: \ abc.txt"); the following example shows how to write files.

FileStream filewriter = new FileStream (@ "C: \ abc.txt", FileMode. Append, FileAccess. Write );

Open the file in append mode and perform write operations.

StreamWriter sw = new StreamWriter (filewriter); constructor;

For (char mychar = 'a'; mychar <= 'Z'; mychar ++)

{

Sw. Write (mychar); // Write lowercase letters from the a-z26 to the file.

}

Sw. Close (); filewriter. Close ();

// Demo

FileStream filest = new FileStream (@ "c: \ abc.txt", FileMode. Open, FileAccess. ReadWrite );

StreamReader sr = new StreamReader (filest );

String strLine = sr. ReadLine ();

While (strLine! = Null)

{

Console. WriteLine (strLine );

StrLine = sr. ReadLine ();

}

Sr. Close ();

Filest. Close ();

   

The above two figures show the comparison between the output content and the content in abc.txt.

 

Fr: http://www.bianceng.cn/Programming/csharp/200906/11379.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.