Various streams in C #

Source: Internet
Author: User

// Stream is an abstract class and cannot be instantiated directly.
// Filestream inherits from stream and reads and writes file streams.
// Streamwriter inherits from textwriter (abstract class, inherited from marshalbyrefobject, idisposable) and can operate stream
// Streamreader inherits from textreader (abstract class, inherited from marshalbyrefobject, idisposable) and can operate stream
// Memorystream inherits from stream
// Bufferedstream inherits from stream

Streamwriter and streamreader can directly read and write files, provided that the file already exists. If no file exists, an exception is thrown.

// File stream operations
Private void btnfilestream_click (Object sender, eventargs E)
{
String Path = @ "C: \ test.txt ";

# Region filestream the read/write file must be in byte format
// Write a stream to the file
Using (filestream FS = file. Create (PATH ))
{
// Byte [] info = new utf8encoding (true). getbytes ("this is some text ");
Byte [] info = encoding. getencoding ("UTF-8"). getbytes ("this is some text ");
FS. Write (Info, 0, info. Length );
}

// Enable file read reflux
Using (filestream FS = file. openread (PATH ))
{
String result = string. empty;
Byte [] info = new byte [1024];
// Utf8encoding temp = new utf8encoding (true );
While (FS. Read (Info, 0, info. Length)> 0)
{
// Result + = temp. getstring (Info );
Result + = encoding. getencoding ("UTF-8"). getstring (Info );
}
System. Diagnostics. Debug. writeline (result );
}

# Endregion

# Region streamwriter writes data in the form of characters. 1. You can write data to the stream. 2. You can also directly write information to the path file (provided that the file already exists)

// Open the file and add information at the end of the file
Filestream = new filestream (path, filemode. append );
Streamwriter Sw = new streamwriter (filestream );
Sw. writeline ("this is the appended line .");
Sw. Close ();
Filestream. Close ();

// Open the file and add information at the end of the file
Using (filestream file = new filestream (path, filemode. append ))
{
Using (streamwriter writer = new streamwriter (File ))
// Using (streamwriter writer = new streamwriter (PATH ))
{
Writer. writeline ("This A the second appended line .");
Writer. Write ("This A the third appended line .");
}
}

// If the file already exists, you can directly use streamwriter to write data without using File

// Using (streamwriter writer = new streamwriter (PATH ))
//{
// Writer. writeline ("This A the second appended line .");
// Writer. Write ("This A the third appended line .");
//}

# Endregion

# Region streamreader 1. It can be read from stream, 2. It can also be read directly from File

// Read File text content 1
Using (streamreader reader = new streamreader (path, encoding. utf8 ))
{
String line;
While (line = reader. Readline ())! = NULL)
{
System. Diagnostics. Debug. writeline (line );
}
}

// Read File text content 2
Using (filestream fs1 = file. openread (PATH ))
{
Using (streamreader reader = new streamreader (fs1, encoding. getencoding ("gb2312 ")))
{
String result = reader. readtoend ();
System. Diagnostics. Debug. writeline (result );
}
}

# Endregion

# Region memory stream, readable and writable. the operation unit is byte. It is mainly used to operate byte streams in memory and can be written to stream.

Using (memorystream MS = new memorystream ())
{
Unicodeencoding uniencoding = new unicodeencoding ();
Byte [] info = uniencoding. getbytes ("this is memorystream ");

// Write information to memorystream
Ms. Write (Info, 0, info. Length );

Byte [] result = new byte [Ms. Length];
// Read information from the stream to the byte array
Ms. Read (result, 0, info. Length );

Using (Stream S = new filestream ("C:/aa.txt", filemode. Create ))
{
// Write the memory stream to the stream: This is memorystream
Ms. writeto (s );
}
}
# Endregion

// The buffer zone is a byte block in the memory used to cache data, thus reducing the number of calls to the operating system. The buffer can improve Reading and Writing Performance.
// Bufferedstream
}

 

Write logs:

Savefiledialog = new savefiledialog ();
Savefiledialog. filter = "log files (*. log) | *. log | TXT files (*. TXT) | *. TXT | all files (*. *) | *. *";
Savefiledialog. filterindex = 0;
Savefiledialog. restoredirectory = true;
Savefiledialog. filename = datetime. Now. tostring ("yyyymmddhhmmss") + ". log ";
Filestream FS = NULL;
Streamwriter Sw = NULL;
If (savefiledialog. showdialog () = dialogresult. OK)
{
Try
{
FS = (filestream) savefiledialog. openfile ();
Sw = new streamwriter (FS );
Sw. Write (txtimportlog. Text );
}
Catch (exception ex)
{
MessageBox. Show ("log export failed! "+ Ex. Message );
}
Finally
{
Sw. Flush ();
Sw. Close ();
FS. Close ();
}
}

 

 

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.