读书人

用BinaryReader与FileStream的Read步骤

发布时间: 2012-07-31 12:33:46 作者: rapoo

用BinaryReader与FileStream的Read方法读出的byte数组有什么不同?
//方法如下,我初步进行比较,好像(test1()和test2())二者没什么区别,byteData中的结果都是一样的吧?
//而且,用BinaryReader(既test)好像是比较标准的写法 既然结果是一样的。

他为什么要用两个类(FileStream BinaryReader)这样不觉得多此一举吗?

C# code
    class Program    {        static void Main(string[] args)        {            test1();            Console.ReadKey();        }        static void test1()        {            //把文件转换成二进制流            byte[] byteData = new byte[100];            FileStream fs = new FileStream(@"c:\1.txt", FileMode.Open, FileAccess.Read);            BinaryReader read = new BinaryReader(fs);            read.Read(byteData, 0, byteData.Length);            foreach (byte b in byteData)            {                Console.Write(" {0} ", b);            }        }        static void test2()        {            //把文件转换成二进制流            byte[] byteData = new byte[100];            FileStream fs = new FileStream(@"c:\1.txt", FileMode.Open, FileAccess.Read);            fs.Read(byteData, 0, byteData.Length);            foreach (byte b in byteData)            {                Console.Write(" {0} ", b);            }        }    }


[解决办法]
当然不一样,stream只是二进制的, BinaryReader 可以用制定编码去对数据进行编码读取,所以他才能直接readstring,readxxxx,而不需要你去定位流和读取长度

读书人网 >C#

热点推荐