File stream usage analysis for IO operations in.NET

  • 2021-06-28 12:12:52
  • OfStack

This article provides an example of file stream usage for IO operations in.NET.Share it for your reference.Specific analysis is as follows:

Read operation

//1. Create File Stream 
FileStream fsRead =new FileStream("1.txt",FileMode.Open);
//2. Creating a buffer normally does not directly equal the file size.It's just reading here, so that's it.
byte[] bytes =new byte[fsRead.Length];
//3. Start reading, The return value is the length read.
int r =fsRead.Read(bytes,0,bytes.Lenght);
//4. Close release stream
fsRead.Close();
fsRead.Dispose();

Write operation

//1. Create a written file stream 
FileStream fsWrite fsWrite =new FileStream(@"xxx",FileMode.OpenOrCreate);
//2. Create Buffers
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 an string and an byte array is often required when writing to a file stream.
Here's a brief description of what you're doing here.

Array from string to byte[].

string msg ="HelloWorld";
// Use UTF8 Code
byte[] bytes =System.Text.Encoding.UTF8.GetByte(msg);
// Use system default encoding
byte[] bytes =System.Text.Encoding.Default.GetByte(msg);


2.byte[] to string
string newMsg =System.Text.Encoding.UTF8.GetString(bytes);

Coding issues

Why is Chinese garbled?
In UTF8 encoding, one Chinese character takes up two bytes.
In GBK encoding, one Chinese character takes up 3 bytes.
In UTF8 encoding, one Chinese character is saved with two bytes. If you read it with GBK, it is read in 3 bytes and one word format.Of course, it's a mess.The reverse is the same.

To sum up, wear a shoe of size 36 on a foot of size 50.Still a foot size of 36 and a shoe size of 50.It doesn't look very comfortable.

So, the format in which you write is what you read.Is the correct solution.

PS:
1.Utf8 is an international standard.
2.GB2312 is the national standard code and supports Chinese.
3.GBK is an extension of GB2312 to support traditional Chinese.

What classes can be Dispose() ?

1. Dispose () means the release of resources. NET has a unified agreement or description for Dispose ().This convention is represented by an interface.

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

So what kind of classes in the class library will implement the IDisposable interface?
My understanding is that, in general, only classes or objects that consume memory resources in the managed heap.1 Dispose() is generally not required.Garbage collection is done.
However, for file handles, network port numbers, database connections, etc., CLR's garbage collection mechanism is irrelevant.
Therefore, in general, this part of the content needs to implement the IDisposable interface.
Exception handling for file stream operations

// Only the handles fs Defined here, finally In order to be referenced. 
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 Unassigned, then direct Dispose A null pointer exception is thrown.
     {
         fs.Dispose();
     }
}

Simplify the above, rigorous but slightly cumbersome.Microsoft provides grammatical sugar.
Is the syntax of using
using( A class that releases resources )
{
      operation
}
//1. When the operation is completed, it is automatically released.
//2.using Once the statement has been compiled, code similar to the one above will be generated.Is to use try  finally .

StreamWriter and StreamReader
// 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();  // Every return 1 Strings

I hope that the description in this paper will be helpful to your.net program design.


Related articles: