zoukankan      html  css  js  c++  java
  • 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有问题的可以在群里面提问。
  • 相关阅读:
    Linux下C程序的反汇编【转】
    数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)【转】
    linux arm的存储分布那些事之一【转】
    linux arm mmu基础【转】
    linux 进程内存解析【转】
    如何更方便的查看Linux内核代码的更新记录【转】
    设备树解析【转】
    分析内核源码,设备树【转】
     Meltdown论文翻译【转】
    device tree --- #address-cells and #size-cells property【转】
  • 原文地址:https://www.cnblogs.com/shiyh/p/14918382.html
Copyright © 2011-2022 走看看