. File flow usage Analysis of IO operation in net-practical skills

Source: Internet
Author: User

This example describes the. NET, the file stream usage of IO operations in the. Share to everyone for your reference. The specific analysis is as follows:

Read operations

Copy Code code as follows:
1. Create a file stream
FileStream fsread =new FileStream ("1.txt", FileMode.Open);
2. Create a buffer that, under normal circumstances, is not directly equal to the file size. It's only read here, so it's done.
byte[] bytes =new byte[fsread.length];
3. Start reading, the return value is read to the length.
int r =fsread.read (bytes,0,bytes. Lenght);
4. Close the release stream
Fsread.close ();
Fsread.dispose ();

Write operations

Copy Code code as follows:
1. Create a written file stream
FileStream fswrite fswrite =new FileStream (@ "xxx", filemode.openorcreate);
2. Create buffer
String msg = "HelloWorld";
Byte[] bytes =enconding.utf8.getbytes (msg);
3. Start writing
Fswrite.write (bytes,0,bytes. Length);
4. Close
Fswrite.close ();
Fswrite.dispose ();

Conversion between a byte array and a string

When a file stream is written, a conversion between a string and a byte array is often required.
Here is a brief description of the practice in this regard.

1.string to byte[] array.

Copy Code code as follows:
String msg = "HelloWorld";
Using UTF8 encoding
Byte[] bytes =system.text.encoding.utf8.getbyte (msg);
Use system default encoding
Byte[] bytes =system.text.encoding.default.getbyte (msg);


2.byte[] to String
Copy Code code as follows:
String newmsg =system.text.encoding.utf8.getstring (bytes);

Coding problems

Why is Chinese garbled?
In UTF8 encoding, a Chinese character occupies two bytes.
In GBK encoding, a Chinese character occupies three bytes.
In UTF8 encoding, a Chinese character is saved in two bytes, and if you read it in GBK, read it in three-byte format. Of course it's garbled. The opposite is the same.

To sum up, whether it is 36 yards of shoes, wear in 50 yards of feet. Or a 36-yard foot and a 50-yard shoe. It doesn't look very comfortable.

So, write in what format, read in what format. is the positive solution.

Ps:
1.utf8 is an international standard.
2.GB2312 is a GB code, supporting the Chinese language.
3.GBK is an extension of GB2312 to support traditional Chinese.

What class can Dispose ()?

1.Dispose () indicates the release of resources. NET has a uniform convention or description for Dispose (). This Convention behaves as an interface.

Or this interface, which is a red-headed, red-headed how to release resources.
All classes that implement the IDisposable interface can be freed and disposed ().

So what classes in the class library implement the IDisposable interface?
My understanding is this, typically, a class or object that consumes only memory resources in the managed heap. No Dispose () is generally required. The garbage collection is done.
However, for file handles, network port numbers, database connections, and so on, the CLR's garbage collection mechanism is regardless.
So generally this part of the content needs to implement IDisposable interface.
exception handling for file flow operations

Copy Code code as follows:
Only the FS definition here, finally can be cited.
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, direct Dispose throws a null pointer exception.
{
Fs. Dispose ();
}
}

Simplifying the above wording, although rigorous but a little bit troublesome. Microsoft has provided syntax sugar.
is the syntax of using
Copy Code code as follows:
Using (a class that can release resources)
{
Operation
}
1. The execution of the operation will be automatically released.
After the 2.using statement is compiled, it will form code similar to the above. is to use try finally.

StreamWriter and StreamReader
Copy Code code as follows:
Write by row
StreamWriter SW =new StreamWriter (@ "target", true,encoding.getenconding ("GB2312"));
Sw. WriteLine ("HelloWorld");

Read by row
StreamReader SR =new StreamReader (@ "Source");
Sr.  Readerline (); Returns a string at a time

I hope this article will help you with your. NET programming.

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.