0505.Net Basic Class | File Class and FileStream Class for File Operation

Keywords: encoding github

Content from: 0505.Net Basic Class | File Class and FileStream Class Contrast of File Class and FileStream Class for File Operation

File management (File class)

Content Links: https://blog.csdn.net/kuai8le/article/details/101418283

 

File Stream

FileStream classes are operation bytes

Read Method and Write Method

FileMode

Read,Write,ReadWrite

FileAccess

Append,Create,CreateNew,Open,

OpenOrCreate,Truncate

Comparing read data

Use File to read data

byte[] buffer = File.ReadAllBytes(@"C:\Users\Administrator.USER-20190915QG\Desktop\Abstract class characteristics.txt");
string str = Encoding.Default.GetString(buffer);
Console.WriteLine(str);
Console.ReadKey();

Use FileStream to read data

FileStream fsRead = new FileStream(@"C:\Users\Administrator.USER-20190915QG\Desktop\Abstract class characteristics.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer = new byte[1024 * 1024 * 5];
//Returns the number of valid bytes actually read this time
int r = fsRead.Read(buffer, 0, buffer.Length);
//Converting byte arrays to strings
string str = Encoding.Default.GetString(buffer, 0, r);
//Closed flow
fsRead.Close();
//Release of Flow Resources
fsRead.Dispose();
Console.WriteLine(str);
Console.ReadKey();

Contrast Write Data

Use File class to write data

string str = "It's a nice day today!";
byte[] buffer = Encoding.Default.GetBytes(str);
File.WriteAllBytes(@"C:\Users\Administrator.USER-20190915QG\Desktop\new.txt", buffer);
Console.WriteLine("Write successfully!");
Console.ReadKey();

Write data using FileStream class

FileStream fsWrite = new FileStream(@"C:\Users\Administrator.USER-20190915QG\Desktop\new2.txt", FileMode.OpenOrCreate, FileAccess.Write);
string str = "I am in a good mood today!";
byte[] buffer = Encoding.Default.GetBytes(str);
fsWrite.Write(buffer, 0, buffer.Length);
fsWrite.Close();
fsWrite.Dispose();
Console.WriteLine("Write successfully!");
Console.ReadKey();

Written in using

Writing the process of creating stream objects in using will automatically help us release the resources occupied by the stream.

FileStream Read

using (FileStream fsRead = new FileStream(@"C:\Users\Administrator.USER-20190915QG\Desktop\Abstract class characteristics.txt", FileMode.OpenOrCreate, FileAccess.Read))
{
    byte[] buffer = new byte[1024 * 1024 * 5];
    int r = fsRead.Read(buffer, 0, buffer.Length);
    string str = Encoding.Default.GetString(buffer, 0, r);
    Console.WriteLine(str);
}
Console.ReadKey();

FileStream Writing

using (FileStream fsWrite=new FileStream(@"C:\Users\Administrator.USER-20190915QG\Desktop\new2.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
    string str = "I am in a good mood today!";
    byte[] buffer = Encoding.Default.GetBytes(str);
    fsWrite.Write(buffer, 0, buffer.Length);
    Console.WriteLine("Write successfully");
}
Console.ReadKey();

https://github.com/Link2019/FileDemo

Posted by ozPATT on Mon, 30 Sep 2019 03:27:09 -0700