随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

C#中流的读写器BinaryReader、BinaryWriter,StreamReader、StreamWriter详解【转】

https://blog.csdn.net/ymnl_gsh/article/details/80723050

C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 StreamReader类增强了FileStream,它让我们在字符串级别上操作文件,但有的时候我们还是需要在字节级上操作文件,却又不是一个字节 一个字节的操作,通常是2个、4个或8个字节这样操作,这便有了BinaryWriter和BinaryReader类,它们可以将一个字符或数字按指定 个数字节写入,也可以一次读取指定个数字节转为字符或数字。


StreamWriter sr=new StreamWriter(@"xxxx.txt");  ->文本读写

FileStream fs=File.Open(@"xxx.dat",FileMode.Open);
BinaryWriter bw=new BinaryWriter(fs););               ->二进制读写

注意,从FileInfo.OpenWrite()返回的FileStream对象被传到BinaryWriter类型的构造函数中。使用这项技术就能很方便地在写入数据前引入一个流。需要理解的是,BinaryWriter构造函数能接受任何派生自Stream类型的参数(比如FileStream、MemoryStream或BufferedStream)。

文本读写器(StreamWriter/Reader)和二进制读写器(Binary Writer/Reader)的关系:

首先两者都是基于流来操作文件进行读写的,但后者是以二进制的方式来进行的,后者写入文件后双击打开会是乱码(该文件后缀为.dat),读写都具体的数据类型来操作流的位置,进行读取的时候要通过具体数据类型读取,例如br.ReadInt32()...;
文本读写器中写入具体文件中的是字符串的文本格式,双击打开该文件是可以阅读的,同时在运用StreamReader对象进行读取时,就不能以具体类型大小来操作流的位置了,而是以字符大小为单位操作流的位置的,读出来的也是一个字符串,而不是具体类型。

 namespace ch16
{
    class Program
    {

        static void txtOperate()  //文本输入输出
        {
            string t = @"F:\test\tt.txt";
            try
            {
                using (StreamWriter sw1 = new StreamWriter(t))
                {
                    sw1.WriteLine("123"); sw1.WriteLine("456");
                    Console.WriteLine("写入成功!");
                }
                using (StreamReader sr=new StreamReader(t))
                {
                    Console.WriteLine("文本内容为:");
                    string line;
                    while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); }
                }
            }
            catch (Exception e) { Console.WriteLine(e.Message); }
        }
        static void binaryRW(string fileName)//二进制文件输入输出
        {
            using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                bw.Write(10);
                bw.Write("hello binary!");
                bw.Write(true);
            }
            using (BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                int i1 = br.ReadInt32();
                string s = br.ReadString();
                bool b = br.ReadBoolean();
                Console.WriteLine("{0},{1},{2}", i1, s, b);

            }
        }
        static void Main(string[] args)
        {
            binaryRW(@"d:\abc.dat");
            Console.Read();
        }
    }
}

posted on   3D入魔  阅读(922)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2016-03-29 BumpMapping [转]
2013-03-29 C++箴言:理解typename的两个含义(转)
2012-03-29 一款不错的开源屏幕(窗口)录制软件 CamStudio
2010-03-29 最常用的开源游戏引擎
2010-03-29 3D图形数学(3D Graphics Math)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示