C#--输入和输出FileStream、BinaryReader和BinaryWriter、StreamReader和StreamWriter总结

为什么要出现与文件流配套的读写器类型呢?
主要是因为文件流对象(FileStream)在读写字节的效率是相当高的,但是在处理其他类型的数据时会比较麻烦,所以就出现了二进制读写器(BinaryReader和BinaryWriter)和文本读写器(StreamReader和StreamWriter)来解决这一问题。

FileStream类:

using System;
using System.IO;

namespace FileIOApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream F = new FileStream("test.dat", 
            FileMode.OpenOrCreate, FileAccess.ReadWrite);

            for (int i = 1; i <= 20; i++)
            {
                F.WriteByte((byte)i);
            }

            F.Position = 0;

            for (int i = 0; i <= 20; i++)
            {
                Console.Write(F.ReadByte() + " ");
            }
            F.Close();
            Console.ReadKey();
        }
    }
}

输出结果:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1

二进制读写器:

using System;
using System.IO;

namespace BinaryFileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            BinaryWriter bw;
            BinaryReader br;
            int i = 25;
            double d = 3.14157;
            bool b = true;
            string s = "I am happy";
            // 创建文件
            try
            {
                bw = new BinaryWriter(new FileStream("mydata",
                FileMode.Create));
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot create file.");
                return;
            }
            // 写入文件
            try
            {
                bw.Write(i);
                bw.Write(d);
                bw.Write(b);
                bw.Write(s);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot write to file.");
                return;
            }

            bw.Close();
            // 读取文件
            try
            {
                br = new BinaryReader(new FileStream("mydata",
                FileMode.Open));
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot open file.");
                return;
            }
            try
            {
                i = br.ReadInt32();
                Console.WriteLine("Integer data: {0}", i);
                d = br.ReadDouble();
                Console.WriteLine("Double data: {0}", d);
                b = br.ReadBoolean();
                Console.WriteLine("Boolean data: {0}", b);
                s = br.ReadString();
                Console.WriteLine("String data: {0}", s);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "\n Cannot read from file.");
                return;
            }
            br.Close();
            Console.ReadKey();
        }
    }
}

输出结果:

Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

文本读写器:

using System;
using System.IO;

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] names = new string[] {"Zara Ali", "Nuha Ali"};
            using (StreamWriter sw = new StreamWriter("names.txt"))
            {
                foreach (string s in names)
                {
                    sw.WriteLine(s);
                }
            }

            // 从文件中读取并显示每行
            string line = "";
            using (StreamReader sr = new StreamReader("names.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
            Console.ReadKey();
        }
    }
}

输出结果:

Zara Ali
Nuha Ali

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BinaryReaderBinaryWriter类是.NET Framework中用于读写二进制数据的类,常用于读写文件或网络数据流。BinaryReader类提供了从二进制数据流中读取各种数据类型的方法,包括整型、浮点型、布尔型、字符串等。而BinaryWriter类则提供了向二进制数据流中写入各种数据类型的方法。 示例代码: ``` //写入二进制文件 using (FileStream stream = new FileStream("data.bin", FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(1); writer.Write("hello"); writer.Write(true); } } //读取二进制文件 using (FileStream stream = new FileStream("data.bin", FileMode.Open)) { using (BinaryReader reader = new BinaryReader(stream)) { int num = reader.ReadInt32(); string str = reader.ReadString(); bool flag = reader.ReadBoolean(); Console.WriteLine(num + " " + str + " " + flag); } } ``` StreamReaderStreamWriter类也是用于读写文件或网络数据流的类,不同的是它们是用于读写文本数据的。StreamReader类提供了从文本数据流中读取文本的方法,而StreamWriter类则提供了向文本数据流中写入文本的方法。 示例代码: ``` //写入文本文件 using (StreamWriter writer = new StreamWriter("data.txt")) { writer.WriteLine("hello"); writer.WriteLine("world"); } //读取文本文件 using (StreamReader reader = new StreamReader("data.txt")) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } ``` 需要注意的是,在使用StreamReaderStreamWriter类时,需要指定编码方式(如UTF-8、GB2312等),否则可能会出现乱码问题。可以在构造函数中指定编码方式,如: ``` using (StreamWriter writer = new StreamWriter("data.txt", false, Encoding.UTF8)) { // ... } using (StreamReader reader = new StreamReader("data.txt", Encoding.UTF8)) { // ... } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值