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 + " Cannot create file.");
                    return;
                }
                // 写入文件
                try
                {
                    bw.Write(i);
                    bw.Write(d);
                    bw.Write(b);
                    bw.Write(s);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + " Cannot write to file.");
                    return;
                }
     
                bw.Close();
                // 读取文件
                try
                {
                    br = new BinaryReader(new FileStream("mydata",
                    FileMode.Open));
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + " 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 + " 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


JAVA&NET技术QQ群号:456257217有问题的可以在群里面提问。
原文地址:https://www.cnblogs.com/shiyh/p/14918382.html