StreamReader implements the abstract base class TextReader class, and StreamWriter implements the abstract base class TextWriter. Used for reading and writing of the stream, respectively.
Let's start with the StreamReader.
First, the construction method
StreamReader Initializes a new instance of the StreamReader class for the specified stream. StreamReader (String) Initializes a new instance of the StreamReader class for the specified file name. StreamReader (Stream, Boolean) Initializes a new instance of the StreamReader class for the specified stream, using the specified byte order mark detection option. StreamReader (Stream, Encoding) Initializes a new instance of the StreamReader class with the specified character encoding for the specified stream. StreamReader (String, Boolean) Initializes a new instance of the StreamReader class for the specified file name with the specified byte order mark detection option. StreamReader (String, Encoding) Initializes a new instance of the StreamReader class for the specified file name, using the specified character encoding. StreamReader (Stream, Encoding, Boolean) Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection options. StreamReader (String, Encoding, Boolean) Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection options. StreamReader (Stream, Encoding, Boolean, Int32) Initializes a new instance of the StreamReader class for the specified stream with the specified character encoding, byte order mark detection options, and buffer size. StreamReader (String, Encoding, Boolean, Int32) Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding, byte order mark detection options, and buffer size.
Second, public properties
BaseStream returns the underlying stream. Currentencoding Gets the current character encoding that is being used by the current StreamReader object.
Iii. Public methods
Close closes the StreamReader object and the underlying stream and frees all system resources associated with the reader. (Rewrite TextReader..::.) Close () () (). Createobjref creates an object that contains all the relevant information that is required to generate a proxy for communicating with a remote object. (inherited from MarshalByRefObject.) Discardbuffereddata allows the StreamReader object to discard its current data. Dispose is overloaded. Getlifetimeservice retrieves the current lifetime service object that controls the lifetime policy of this instance. (inherited from MarshalByRefObject.) InitializeLifetimeService Gets the lifetime service object that controls the lifetime policy of this instance. (inherited from MarshalByRefObject.) ) MemberwiseClone is overloaded. Peek returns the next available character, but does not use it. (Rewrite TextReader..::.) Peek () () (). Read is overloaded. Reads the next character or the next set of characters in the input stream. Readblock reads the maximum count character from the current stream and writes the data to buffer starting at index. (inherited from TextReader.) ReadLine reads a line of characters from the current stream and returns the data as a string. (Rewrite TextReader..::.) ReadLine () () (). )
The following is a sample code for the StreamReader class:
Staticvoid Main (String[] args) {FileStream fs =New FileStream (@"D:\text.txt", FileMode.Open, FileAccess.Read); StreamReader sr =NewStreamReader (FS); Stream stream = Sr. BaseStream;//Returns the base stream Console.WriteLine (stream. GetType ());//Output System.IO.FileStream Console.WriteLine (Sr. currentencoding);//The encoding that is used by the current stream reader, which defaults to native encoding (when the constructor is not specified), and if the constructor method specifies, the output is the encoding Console.WriteLine (SR) specified by the constructor method. Endofstream);//Output False Indicates whether the position of the current stream is at the end of the streamString str = Sr. ReadToEnd ();//This method is useful for streams that cannot use length, such as a compressed stream reading from the current position to the end of the stream Console.WriteLine (str);//Output the contents of the D:\text.txt textFs. Seek (3, Seekorigin.begin);//Cheap start for 3,utf8,1 bytes takes 4 characters (0,1,2,3)String str1 = Sr. ReadLine ();//Output D:\text.txt first line of textConsole.WriteLine (STR1);String str2 =Sr. ReadLine (); Console.WriteLine (STR2);//Output D:\text.txt second line of textConsole.WriteLine ("-----------------------"); Fs. Seek (3, Seekorigin.begin);Char[] chars =Newchar[10]; Sr. Read (chars,0,10);//Reads the first 11 characters into an array of characters (starting from 0)for (int i =0; I < chars. Length; i++) {Console.WriteLine (chars[i]);} Console.WriteLine ("=============="); Console.WriteLine (Convert.tochar (Sr. Read ()));//The output number, which is the next character after being converted to a characterusing (StreamReader SR1 =New StreamReader (@"D:\text.txt", Encoding.UTF8)) {int s =0;while (s = SR1. Read ())! =-1)// note to use an int s to record the characters read, because each time the read () method is called, the position advances one {Console.Write (Convert.tochar (s)); // output all characters }} Console.WriteLine ( ".................. "); using (StreamReader SR3 = new StreamReader (@ "d:\text.txt" // first word of output text } Console.readkey (); }
StreamWriter class
First, the attribute
AutoFlush Gets or sets a value that indicates whether StreamWriter calls StreamWriter every time ...::. After Write, its buffer is flushed to the underlying stream.
BaseStream gets the underlying stream that is connected to the backing store.
Encoding gets the Encoding to which the output is written. (Rewrite TextWriter..::.) Encoding. )
FormatProvider gets the object that controls the format setting. (inherited from TextWriter.) )
NewLine Gets or sets the line terminator string used by the current TextWriter. (inherited from TextWriter.) )
Second, the method
Close closes the current StreamWriter object and the underlying stream. (Rewrite TextWriter..::.) Close () () (). )
Createobjref creates an object that contains all the relevant information that is required to generate a proxy for communicating with a remote object. (inherited from MarshalByRefObject.) )
Dispose is overloaded.
Finalize is overloaded.
Flush clears all buffers for the current writer and causes all buffered data to be written to the underlying stream. (Rewrite TextWriter..::.) Flush () () () (). )
Getlifetimeservice retrieves the current lifetime service object that controls the lifetime policy of this instance. (inherited from MarshalByRefObject.) )
InitializeLifetimeService Gets the lifetime service object that controls the lifetime policy of this instance. (inherited from MarshalByRefObject.) )
MemberwiseClone is overloaded.
Write is overloaded. Writes a stream.
WriteLine is overloaded.
StreamWriter Class Example:
Staticvoid Main (String[] args) {StreamWriter SW =New StreamWriter (@"D:text.txt"); Console.WriteLine (SW. AutoFlush);//Output False if the buffer is flushed to the underlying stream Console.WriteLine (SW) after each call to SW. Basestream.gettype ());//The base stream of the output FileStream Direct write path is FileStream Console.WriteLine (SW). Encoding);//System.Text.UTF8Encoding Console.WriteLine (SW. formatprovider);//ZH-CN Console.WriteLine (SW. Newline.tostring ());// "123123123 "); // write one line at a time SW. Flush (); // There is a buffer to force data in the output buffer before the SW is really displayed. WriteLine ( "123123123 "); Sw. Flush (); Sw. Write ( " test test test test test testing " ); //
Today there is a very interesting demand, the demand is too long to say unclear. But it can be understood that only the first line of text needs to be read. Because if the entire text is read out and then gets the first line, the memory is more expensive. Because that text is a lot more than a few m large. Therefore, the StreamReader ReadLine () method is used. The demo is as follows:
class program {static void Main (string[] args) {using (FileStream fs = new FileStream (@ "d:\sql.txt" new StreamReader (FS); string line1 = Sr. ReadLine (); Console.WriteLine (line1); // output 111111111111} Console.readkey (); } }
The text is read as follows:
111111111111222222222222333333333333444444444444555555555555
Operation of the file stream