Introduction to C # Streaming write class StreamWriter

Source: Internet
Author: User
Tags flushes

Introduction to stream Write Class StreamWriter in C # (GO)

Applying the FileStream class requires a lot of extra data type conversions, which can be very inefficient. Using the StreamWriter class will provide a simpler and more convenient way to operate.
The StreamWriter class allows characters and strings to be written directly to a file, typically not for binary data.
Structure of the class:
public class Streamwriter:textwriter: Implements a TextWriter that writes characters to a stream in a specific encoding. StreamWriter is designed to output characters in a specific encoding (using UTF8Encoding by default).
Builder:
Public StreamWriter (Stream stream): Initializes a new instance of the StreamWriter class for the specified stream with UTF-8 encoding and default buffer size. The stream of the parameter stream (which can be substituted with the FileStream class instance) to write. --Use FileStream to better control how files are manipulated.
Public StreamWriter (String path): Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. The full file path to which the parameter path is to be written. Path can be a file name.
Public StreamWriter (Stream stream, Encoding Encoding): Initializes a new instance of the StreamWriter class for the specified stream, with the specified encoding and default buffer size. The parameter encoding specifies the character encoding to use.


Public StreamWriter (string path, bool append): Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be overwritten or appended to it. If the file does not exist, this constructor creates a new file. The parameter path specifies the full file path to write. APPEND specifies whether to append data to the file. If the file exists and append is False, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.
Property
Public virtual bool AutoFlush {get; set;} : Gets or sets a value that indicates whether StreamWriter flushes its buffers to the underlying stream after each call to StreamWriter.Write. This property value is true if StreamWriter flushes its buffer, otherwise, false.
Public virtual string NewLine {get; set;} : Gets or sets the line terminator string used by the current TextWriter. The default line terminator string is a carriage return followed by a newline character ("\ r \ n").
Method:
public override void Close (): Closes the current StreamWriter object and the underlying stream.
public override void Flush (): Clears all buffers for the current writer and writes all buffered data to the underlying stream. Note: After writing the data, you must use this method to write out the data in the buffer to the target of the stream.
public override void Write (string value): Writes a string to the stream, and the parameter value is the string to write to the inflow. If value is empty, no content is written.
public virtual void Write (string format, params object[] arg): Writes the formatted string using the same semantics as String.Format. The parameter format formatted string. ARG specifies an array of objects to write to the formatted string.
public virtual void WriteLine (): Writes the line terminator to the text stream. The default line terminator is a carriage return followed by a newline character ("\ r \ n"), but this value can be changed using the NewLine property.
public virtual void WriteLine (string value): Writes a string followed by a line terminator to the text stream. The parameter value specifies the string to write. If value is a null reference (Nothing in Visual Basic), only the line end character is written.
public virtual void WriteLine (string format, params object[] arg): Writes out the formatted string and a new line using the same semantics as format. The parameter format formatted string. ARG specifies an array of objects to write to the formatted string.
Example:
Using System;
Using System.IO;
Using System.Text;
Class TESTRW
{
public static void Main (string[] args)
{
if (File.exists (@ "C:\MyFile.txt"))
{
using (FileStream fs = new FileStream (@ "C:\MyFile.txt", Filemode.append, FileAccess.Write))
{
Fs. Lock (0, FS. Length);
StreamWriter SW = new StreamWriter (FS);
Sw. WriteLine ("I am the best, though not now");
Sw. WriteLine ("There is information, not necessarily will win, but no confidence, will certainly lose");
Sw. WriteLine ("My expected monthly salary is {0,9:C4}, achieving age of {1,9}, Success rate {2,9:P2}", new object[] {20000, 30, 1});
Fs. Unlock (0, FS. LENGTH);//must be used before the flush () method, otherwise throws an exception.
Sw. Flush ();
}
}
Else
{
Console.WriteLine ("File does not exist");
}
}
}

string html = "@006|销售代表|[email protected]|客户代表|[email protected]" ;        string [] list = html.ToLower().Split( ‘|‘ );        string result = "" ;        for ( int i = 0; i < list.Length; i++)        {            if (list.Trim().Contains( "@" ) || list.Trim().Contains( "其他" ))            {            }            else            {                result += list.Trim() + "\r\n" ;            }        }        StreamWriter sw = new StreamWriter( "D:\\abc.xls" , false , System.Text.Encoding.UTF8);        sw.WriteLine(result);        sw.Close();

Introduction to C # Streaming write class StreamWriter

Related Article

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.