In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about the usage analysis of IO operation file stream in .NET, which interested friends may wish to have a look at. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the usage analysis of IO operation file stream in .NET".
This paper gives an example of the file stream usage of IO operation in. Net. Share it with you for your reference. The specific analysis is as follows:
Read operation
The copy code is as follows:
/ / 1. Create a file stream
FileStream fsRead = new FileStream ("1.txt", FileMode.Open)
/ / 2. Creating a buffer, normally, is not directly equal to the file size. There's only reading here, so I did it.
Byte [] bytes = new byte [fsRead.Length]
/ / 3. Starts reading, and the return value is the length read.
Int r = fsRead.Read (bytes,0,bytes.Lenght)
/ / 4. Turn off the release stream
FsRead.Close ()
FsRead.Dispose ()
Write operation
The copy code is 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 byte array and string
Conversion between string and byte arrays is often required when a file stream is written.
Here is a brief description of the practice in this area.
1.string to byte [] array.
The copy code is as follows:
String msg = "HelloWorld"
/ / use UTF8 encoding
Byte [] bytes = System.Text.Encoding.UTF8.GetByte (msg)
/ / use the system default encoding
Byte [] bytes = System.Text.Encoding.Default.GetByte (msg)
2.byte [] to string
The copy code is as follows:
String newMsg = System.Text.Encoding.UTF8.GetString (bytes)
Coding problem
Why are there garbled codes in Chinese?
In UTF8 coding, a Chinese character takes up two bytes.
In GBK coding, a Chinese character takes up three bytes.
In UTF8 coding, a Chinese character is saved with two bytes. If you read it with GBK, read it in a three-byte format. Of course it's garbled. And vice versa.
To sum up, whether it's size 36 shoes, wear size 50 feet. Or size 36 feet, wear size 50 shoes. It doesn't even look very comfortable.
Therefore, read according to the format in which it is written. That's the right answer.
PS:
1.Utf8 is an international standard.
2.GB2312 is a national standard code and supports Chinese.
3.GBK is an extension of GB2312 and supports traditional Chinese.
What class can Dispose ()?
1.Dispose () means to release resources, and there is a unified convention or description for Dispose () in .NET. 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 you can Dispose ()
So what kind of class in the class library implements the IDisposable interface?
My understanding is that classes or objects that generally occupy only memory resources in the managed heap. Dispose () is generally not required. Garbage collection is done.
However, CLR's garbage collection mechanism does not care about file handles, network port numbers, database connections, and so on.
So generally this part of the content needs to implement the IDisposable interface.
Exception handling of file stream operation
The copy code is as follows:
/ / only if fs is defined here can it be referenced in finally.
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, then the direct Dispose throws a null pointer exception.
{
Fs.Dispose ()
}
}
Simplify the above way of writing, although rigorous but a little bit troublesome. Microsoft provides grammatical sugar.
It's the syntax of using.
The copy code is as follows:
Using (a class that can release resources)
{
Operation
}
/ / 1. After the operation is executed, it will be released automatically.
After the / / 2.using statement is compiled, code similar to the above will be formed. Is to use try finally.
StreamWriter and StreamReader
The copy code is as follows:
/ / write by line
StreamWriter sw = new StreamWriter (@ "target", true,Encoding.GetEnconding ("GB2312"))
Sw.WriteLine ("HelloWorld")
/ / read by line
StreamReader sr = new StreamReader (@ "Source")
Sr.ReaderLine (); / / returns one string at a time
At this point, I believe you have a deeper understanding of the usage analysis of IO operation file stream in .NET, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.