Read operation of C# data stream

Read operation of C# data stream

Data streams generally have read and write operations.
1. Read (Read) operation: read the data in the stream object and store it in another data structure.
2. Write operation: read data from another data structure and store it in the stream object.

1. Read and write all file types

The file stream (FileStream) class is used to read and write files.

There are many forms of construction of the FileStream class, the most commonly used one is as follows:

public FileStream(string path, FileMode mode, FileAccess access);
其中,path表示将要操作的文件路径,而参数mode和access都是一个枚举值。

Enumeration members of the FileMode class:

enumeration value File Existence Description file does not exist
Append open and position end create new file
Create rewrite file create new file
CreateNew abnormal create new file
Open open a file abnormal
OpenOrCreate open a file create new file
Truncate truncate the file to 0 bytes abnormal

Enumeration members of the FileAccess class:

enumeration value illustrate
Read read-only mode
Write write-only mode
ReadWrite read-write mode

Copy the C# Getting Started Basics.pdf file case :

string path = @"C:\Users\Administrator\Desktop\C#入门基础.pdf";
string copyPath = @"C:\Users\Administrator\Desktop\C#入土教程.pdf";
FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
FileStream fsWriter = new FileStream(copyPath, FileMode.OpenOrCreate, FileAccess.Write);
while (true)
{
    
    
    byte[] buffer = new byte[1024 * 1024 * 5];//一次读取5M内容
    int r = fsRead.Read(buffer, 0, buffer.Length);//实际读取的有效字节数

    if (r == 0)//读到最后内容
    {
    
    
        break;
    }

    //以ANSI默认格式读取文本内容
    //string tempStr = Encoding.Default.GetString(buffer, 0, r);
    //Console.WriteLine(tempStr);

    fsWriter.Write(buffer, 0, r);
}
//关闭流
fsWriter.Close();
//释放流占用的资源
fsWriter.Dispose();
//关闭流
fsRead.Close();
//释放流占用的资源
fsRead.Dispose();

Console.WriteLine("复制完成.");

C# uses [ using ] to automatically close the stream and release the resources occupied by the stream.

string path = @"C:\Users\Administrator\Desktop\C#入门基础.pdf";
string copyPath = @"C:\Users\Administrator\Desktop\C#入土教程.pdf";

//将创建文件流对象的过程写在using当中,会自动关闭流并释放流占用的资源
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
    
    
    using (FileStream fsWrite = new FileStream(copyPath, FileMode.OpenOrCreate, FileAccess.Write))
    {
    
    
        while(true)
        {
    
    
            byte[] buffer = new byte[1024 * 1024 * 5];
            int r = fsRead.Read(buffer, 0, buffer.Length);

            if(r == 0)
            {
    
    
                break;
            }

            fsWrite.Write(buffer, 0, r);
        }
    }
}

Console.WriteLine("复制完成.");

2. Reading and writing text files

For text files, the following two classes are usually used for processing.
The StreamReader class can directly read the contents of text files.
The StreamWriter class allows characters and strings to be written directly to files, so it is generally used to write text files.

StreamRead reads text files:

string path = @"C:\Users\Administrator\Desktop\a.txt";
//采用默认ANSI编码格式进行读取
using (StreamReader sr = new StreamReader(path, Encoding.Default))
{
    
    
    //在读取末端进行判断,并循环读取
    while (!sr.EndOfStream)
    {
    
    
        //输出每一行读取的内容
        Console.WriteLine(sr.ReadLine());
    }
}

StreamWriter writes to a text file:

string path = @"C:\Users\Administrator\Desktop\a.txt";
//第二参数:默认false,可以不写,表示覆盖内容。true表示为文件追加内容
using (StreamWriter sw = new StreamWriter(path, true))
{
    
    
    sw.Write("追加内容");
    sw.WriteLine("追加内容");
}
Console.WriteLine("写入完成.");

3. Reading and writing binary files

For binary files, the following two classes are usually used for processing.
The BinaryReader class is used for reading binary files, and the processing must be binary files.
The BinaryWriter class is used for writing binary files. The file written may not be a binary file.

BinaryReader reads binary files:

string path = @"C:\Users\Administrator\Desktop\a.txt";
using (BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.OpenOrCreate)))
{
    
    
    bw.Write("写入内容.");
}
Console.WriteLine("写入成功");

BinaryWriter writes to the file:

string path = @"C:\Users\Administrator\Desktop\a.txt";
using (BinaryReader br = new BinaryReader(new FileStream(path, FileMode.OpenOrCreate)))
{
    
    
    Console.WriteLine(br.ReadString());
}

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/123442509