Streamwriter class
07:11:02 Source: Network-collected articles Author: Unknown click rate:
Core Tip: streamwriter is a class used to process text files. It can easily write strings to text files. It is also responsible for important conversion and processing of writing data to filestream objects.
Streamwriter is a class used to process text files. It can easily write strings to text files. It is also responsible for important conversion and processing of writing data to filestream objects.
Common attributes of the streamwriter class are shown in the table.
Attribute |
Description |
Encoding |
Obtain the encoding in which the output is written. |
Formatprovider |
Get the object of control format settings |
Basestream |
Obtain the basic stream connected to the same backup storage area |
Autoflush |
Sets a value indicating whether streamwriter refreshes the buffer to the base stream after streamwriter. Write is called each time. |
Newline |
Obtains or sets the row Terminator string used by the current textwriter. |
The following describes the important attributes of the streamwriter class in detail.
(1) autoflush attributes
Whether streamwriter refreshes the buffer to the base stream after streamwriter. Write is called every time
Syntax:
Public Virtual bool autoflush {Get; set ;} |
Property value:
Force streamwriter to refresh its buffer.
(2) newline attributes
Syntax:
Public Virtual string newline {Get; set ;} |
Attribute Value: the row Terminator string of the current textwriter.
Common streamwriter class methods are shown in the table.
Method |
Description |
Close |
Close the current stringwriter and basic stream |
Createobjref |
Create an object that contains all the information required to generate a proxy for communication with a remote object |
Equals |
Determine whether two object instances are equal |
Flush |
Clears all the buffers of the current writer so that all the buffered data is written to the basic device. |
Getlifetimeservice |
Retrieve the current lifetime service object that controls the lifetime policy of this instance |
Getstringbuilder |
Return to basic stringbuilder |
GetType |
Obtains the type of the current instance. |
Initializelifetimeservice |
Gets the lifetime service object that controls the lifetime policy of this instance. |
Referenceequals |
Determine whether the specified object instance is the same |
Synchronized |
Creates thread-safe packaging around a specified textwriter |
Tostring |
Returns the string containing the characters written to the current stringwriter so far. |
Write |
Written to this instance of stringwriter |
Writeline |
Write some data specified by the overload parameter, followed by the row Terminator |
The following describes the important methods in the streamwriter class in detail.
(1) Close Method
Close the current stringwriter and basic stream.
Syntax:
Public override void close () |
After creating and using a stringwriter, you must immediately disable it.
(2) flush Method
Clears all the buffers of the current writer so that all the buffered data is written to the basic device.
Syntax:
Public Virtual void flush () |
Call the flush method before calling the close method to copy any data previously written into the buffer zone to the file.
(3) writeline Method
Write the row terminator to the text stream.
Syntax:
Public Virtual void writeline () |
Return Value: the default line terminator is the carriage return ("/R/N") followed by the line break, but this value can be changed using the newline attribute.
Streamwriter has two most important and commonly used methods: the write method and the writeline method. Both methods are used to write strings to text files, but they are also different. The writeline () method is only used for strings, and a line break (carriage return/line feed) is automatically appended ). The write () method does not append line breaks. You can write strings to text streams, or you can write text representations of any basic data type.
Note: When Using streamwriter, do not forget to call the close () method to close it after use. When instantiating a streamwriter class object, you can use the using () statement to instantiate a streamwriter class object, which can be automatically released after use. Therefore, you can remove the close () method.