File stream Usage Analysis for IO operations in. NET,. netio

Source: Internet
Author: User

File stream Usage Analysis for IO operations in. NET,. netio

This article describes the usage of file streams for IO operations in. NET. Share it with you for your reference. The specific analysis is as follows:

Read operations
Copy codeThe Code is as follows: // 1. Create a file stream
FileStream fsRead = new FileStream ("1.txt", FileMode. Open );
// 2. Create a buffer. Normally, it is not directly equal to the file size. Here, only reading is required.
Byte [] bytes = new byte [fsRead. Length];
// 3. Start reading. The returned value is the read length.
Int r = fsRead. Read (bytes, 0, bytes. Lenght );
// 4. Close the release stream
FsRead. Close ();
FsRead. Dispose ();

Write operation
Copy codeThe Code is as follows: // 1. Create the written file stream
FileStream fsWrite = new FileStream (@ "xxx", FileMode. OpenOrCreate );
// 2. Create a buffer
String msg = "HelloWorld ";
Byte [] bytes = Enconding. UTF8.GetBytes (msg );
// 3. Start writing
FsWrite. Write (bytes, 0, bytes. Length );
// 4. Disable
FsWrite. Close ();
FsWrite. Dispose ();

Byte array and string Conversion

Conversion between string and byte arrays is often required when writing a file stream.
Here is a brief description of this practice.

1. string to byte [] array.
Copy codeThe Code is as follows: string msg = "HelloWorld ";
// Use UTF8 Encoding
Byte [] bytes = System. Text. Encoding. UTF8.GetByte (msg );
// Use the default system encoding.
Byte [] bytes = System. Text. Encoding. Default. GetByte (msg );
 
2. byte [] to string
Copy codeThe Code is as follows: string newMsg = System. Text. Encoding. UTF8.GetString (bytes );

Encoding Problems

Why are Chinese characters garbled?
In UTF-8 encoding, a Chinese character occupies two bytes.
In GBK encoding, a Chinese character occupies three bytes.
In UTF-8 encoding, one Chinese character is saved in two bytes. If you use GBK to read data, read data in the format of three bytes and one word. Of course it's garbled. The opposite is true.

To sum up, whether it's a 36-yard shoe, wear it on a 50-yard ankle. Or a 36-yard ankle, wearing 50-yard shoes. It does not look very comfortable.

Therefore, the format in which the data is written is read. Is the correct solution.

PS:
1. Utf8 is an international standard.
2. GB2312 is a national standard code that supports Chinese characters.
3. GBK is an extension of GB2312 and supports traditional Chinese.

What classes can be Dispose ()?

1. Dispose () indicates releasing resources. In. NET, there is a unified convention or description for Dispose. This Convention is represented as an interface.

Or this interface is a red header file that specifies how to release resources.
All classes that implement the IDisposable interface can be released and can be Dispose ();

So what classes in the class library will implement the IDisposable interface?
In my understanding, it is generally used only for classes or objects that host the memory resources in the heap. Generally, Dispose () is not required (). Garbage collection is done.
However, for file handles, network port numbers, database connections, etc., the CLR garbage collection mechanism does not matter.
Therefore, you need to implement the IDisposable interface.
Exception Handling for file stream operations
Copy codeThe Code is as follows: // It can be referenced in finally only when fs is defined here.
FileStream fs = null;
Try
{
Fs = new FileStream (@ "file path", FileMode. Create );
Byte [] bytes = Encoding. Default. GetBytes ("HelloWorld ");
Fs. Write (bytes, 0, byte. Length );
}
Finally
{
If (fs! = Null) // If fs is not assigned a value, directly Dispose will cause a null pointer exception.
{
Fs. Dispose ();
}
}
It is a little complicated to simplify the above writing. Microsoft provides syntactic sugar.
Is the using syntax.
Copy codeThe Code is as follows: using (a class that can release resources)
{
Operation
}
// 1. After the operation is completed, it will be automatically released.
// 2. After the using statement is compiled, the code similar to the preceding one will be formed. Try finally.
StreamWriter and StreamReader
Copy codeThe Code is as follows: // write data by row
StreamWriter sw = new StreamWriter (@ "target", true, Encoding. GetEnconding ("GB2312 "));
Sw. WriteLine ("HelloWorld ");
 
// Read by row
StreamReader sr = new StreamReader (@ "Source ");
Sr. ReaderLine (); // each time a string is returned

I hope this article will help you with the. net program design.

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.