Web hosting script - CHAPTER 16 534 THE SYSTEM.IO NAMESPACE To
CHAPTER 16 534 THE SYSTEM.IO NAMESPACE To aid in your understanding of the core writing capabilities of the StreamWriter and StringWriter classes, Table 16-7 describes the core members of the abstract TextWriter base class. Table 16-7. Core Members of TextWriter Member Meaning in Life Close() Closes the writer and frees any associated resources. In the process, the buffer is automatically flushed. Flush() Clears all buffers for the current writer and causes any buffered data to be written to the underlying device, but does not close the writer. NewLine Indicates the newline constant for the derived writer class. The default line terminator is a carriage return followed by a line feed (rn). Write() Writes a line to the text stream without a newline constant. WriteLine() Writes a line to the text stream with a newline constant. Note The last two members of the TextWriter class probably look familiar to you. If you recall, the System. Console type has Write() and WriteLine() members that push textual data to the standard output device. In fact, the Console.In property wraps a TextWriter, and the Console.Out property wraps a TextReader. The derived StreamWriter class provides an appropriate implementation for the Write(), Close(), and Flush() methods, and it defines the additional AutoFlush property. This property, when set to true, forces StreamWriter to flush all data every time you perform a write operation. Be aware that you can gain better performance by setting AutoFlush to false, provided you always call Close() when you are done writing with a StreamWriter. Writing to a Text File Now for an example of working with the StreamWriter type. The following class creates a new file named reminders.txt using the File.CreateText() method. Using the obtained StreamWriter object, you add some textual data to the new file, as shown here: Figure 16-7. Readers and writers
In case you need quality webspace to host and run your web applications, try our personal web hosting services.