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