File stream and. netio operations for IO operations in. NET
Read operations
// 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
// 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
/* Conversions between string and byte arrays are often required when writing a file stream.
Here is a brief description of this practice. */
1. string to byte [] array.
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
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
// 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 ();
}
}
SimplifiedAlthough the above statement is rigorous, it is a little troublesome. Microsoft provides syntactic sugar. Is the using syntax 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
// 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