【读写txt文件】

电脑手机上有各种各样的文件,例如视频文件、图片文件、文本文件,其中读写txt文件是最简单的,有多种方式,

使用StreamReader和StreamWriter

//读取文件
string path = @"C:\example.txt"; // 文件路径
using (StreamReader reader = new StreamReader(path))//使用using语句来确保资源被正确释放,以避免资源泄漏
{
    string line;
    while ((line = reader.ReadLine()) != null) // 逐行读取文件内容,每次读取一行,读取到末尾的时候为空
    {
        Console.WriteLine(line); // 输出每行内容到控制台
    }
}

//写入文件
string path = @"C:\example.txt"; // 文件路径
using (StreamWriter writer = new StreamWriter(path))
{
    string content = "Hello, World!"; // 要写入文件的内容
    writer.WriteLine(content); // 写入一行内容到文件
}

使用TextReader和TextWriter 

//读取文件
string path = @"C:\example.txt"; // 文件路径
using (TextReader reader = new StreamReader(path))
{
    string line;
    while ((line = reader.ReadLine()) != null) // 逐行读取文件内容
    {
        Console.WriteLine(line); // 输出每行内容到控制台
    }
}

//写入文件
string path = @"C:\example.txt"; // 文件路径
using (TextReader reader = new StreamReader(path))
{
    string line;
    while ((line = reader.ReadLine()) != null) // 逐行读取文件内容
    {
        Console.WriteLine(line); // 输出每行内容到控制台
    }
}

 使用FileStream

//读取文件
string path = @"C:\example.txt"; // 文件路径
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    using (StreamReader reader = new StreamReader(fs))
    {
        string line;
        while ((line = reader.ReadLine()) != null) // 逐行读取文件内容
        {
            Console.WriteLine(line); // 输出每行内容到控制台
        }
    }
}


//写入文件
string path = @"C:\example.txt"; // 文件路径
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
    using (StreamWriter writer = new StreamWriter(fs))
    {
        string content = "Hello, World!"; // 要写入文件的内容
        writer.WriteLine(content); // 写入一行内容到文件
    }
}

使用File类提供的静态方法

上面几种方法代码都很长,一般来说我们几乎不会使用这些方法,使用File类提供的静态方法更便捷一些。如果文件非常大,还是要用上面的方法读文件,否则一次性读进来,内存会爆。

如果期望追加写入,还是要用上面的方法,StreamWriter的参数设置为true就好,例如StreamWriter sw = new StreamWriter(filePath, true);

//读取文件
string path = @"C:\example.txt"; // 文件路径
string content = File.ReadAllText(path); // 读取整个文件内容
Console.WriteLine(content); // 输出文件内容到控制台

//写入文件
string path = @"C:\example.txt"; // 文件路径
string content = "Hello, World!"; // 要写入文件的内容
File.WriteAllText(path, content); // 将内容写入文件

//上面是一次性读取写入,得到的对象是一个非常大的string,但有时我们需要逐行处理,就要逐行读取写入
string path = @"C:\example.txt"; // 文件路径
string[] lines = File.ReadAllLines(path); // 读取所有行
foreach (string line in lines)
{
    Console.WriteLine(line); // 在控制台输出每一行
}

string path = @"C:\example.txt"; // 文件路径
string[] lines = { "第一行内容", "第二行内容", "第三行内容" }; // 字符串数组,每个元素将成为一行
File.WriteAllLines(path, lines); // 将字符串数组中的所有元素写入到文件中

文本编码

如果读出来的内容是乱码,建议了解文本编码,在读取和写入文本时都可以传递文本编码参数。

【读写二进制文件】 

使用BinaryWriter和BinaryReader

我们知道在磁盘上只会存储二进制数据,文本文件最后也会被保存为二进制文件,我们调用接口读取和写入时虽然用的是string,但到底层一定是byte[]。这就涉及到string到byte[]的编码和byte[]到string的解码,只不过对于文本文件而言,有确定的编码解码规则,我们不需要关心。

而读写二进制文件,需要你自己设置编码解码规则。编码解码的类型不止是string,可能是各种类型的。

//写入文件
using (BinaryWriter writer = new BinaryWriter(File.Open("file.bytes", FileMode.Create)))
{    
    bool boolValue = true;
    writer.Write(boolValue);

    int intValue = 123;
    writer.Write(intValue);

    float floatValue = 3.14f;
    writer.Write(floatValue);

    double doubleValue = 3.1415926;
    writer.Write(doubleValue);

    char charValue = 'A';
    writer.Write(charValue);

    string content = "永恒之星";
    byte[] bytes = Encoding.UTF8.GetBytes(content);
    writer.Write(bytes.Length); // 写入字符串长度
    writer.Write(bytes); // 写入字符串字节数组
}

 读取文件时按照写入的顺序一个个读取即可。

//读取文件
        using (BinaryReader reader = new BinaryReader(File.Open("file.bytes", FileMode.Open)))
        {
            bool boolValue = reader.ReadBoolean();
            Debug.Log(boolValue);

            int intValue = reader.ReadInt32();
            Debug.Log(intValue);

            float floatValue = reader.ReadSingle();
            Debug.Log(floatValue);

            double doubleValue = reader.ReadDouble();
            Debug.Log(doubleValue);

            char charValue = reader.ReadChar();
            Debug.Log(charValue);

            int length = reader.ReadInt32();
            byte[] bytes = reader.ReadBytes(length);
            string content = Encoding.UTF8.GetString(bytes);
            Debug.Log(content);
        }

使用MemoryStream

上面的问题在于是一次次读写的,会有多次IO开销,可以借助MemoryStream将文件内容先一次性读写到内存中,然后再读写到磁盘中。

另外,我们读写数据时是一行行代码读写的,如果有一万个数据,当然不可能写一万行代码,所以对同类型的数据需要将数据个数也写入,用for循环减少代码量。

        //写入文件
        using (MemoryStream ms = new MemoryStream())
        {
            using (BinaryWriter writer = new BinaryWriter(ms))
            {

                bool boolValue = true;
                writer.Write(boolValue);

                int count = 5;
                writer.Write(count);
                for (int i = 0; i < count; i++)
                {
                    int intValue = 123;
                    writer.Write(intValue);
                }

                float floatValue = 3.14f;
                writer.Write(floatValue);

                double doubleValue = 3.1415926;
                writer.Write(doubleValue);

                char charValue = 'A';
                writer.Write(charValue);

                string content = "永恒之星";
                byte[] bytes = Encoding.UTF8.GetBytes(content);
                writer.Write(bytes.Length); // 写入字符串长度
                writer.Write(bytes); // 写入字符串字节数组
            }
            File.WriteAllBytes("file.bytes", ms.ToArray());//一次性写入磁盘
        }

        //读取文件
        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes("file.bytes")))//一次性从磁盘读取数据
        {
            using (BinaryReader reader = new BinaryReader(ms))
            {
                bool boolValue = reader.ReadBoolean();
                Debug.Log(boolValue);

                int count = reader.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    int intValue = reader.ReadInt32();
                    Debug.Log(intValue);
                }

                float floatValue = reader.ReadSingle();
                Debug.Log(floatValue);

                double doubleValue = reader.ReadDouble();
                Debug.Log(doubleValue);

                char charValue = reader.ReadChar();
                Debug.Log(charValue);

                int length = reader.ReadInt32();
                byte[] bytes = reader.ReadBytes(length);
                string content = Encoding.UTF8.GetString(bytes);
                Debug.Log(content);
            }
        }

拓展

如果我们有很多不同类型的数据需要写到不同的二进制文件中,而且后期的数据结构还可能会修改,那么每次自己读写就非常繁琐,维护成本很高。我们需要借助些序列化工具帮忙读写,我们只需要定义好数据结构,并填充数据即可。这些工具有BinaryFomatter、ProtoBuf、FlatBuffer,更具需求选择合理的工具。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐