为什么需要添加File.OpenRead BinaryReader但不在StreamReader中? [英] Why It Is Require To To Add File.OpenRead BinaryReader But Not In StreamReader ?

查看:52
本文介绍了为什么需要添加File.OpenRead BinaryReader但不在StreamReader中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要添加File.OpenRead BinaryReader但不在StreamReader中?



BinaryReader ll = new BinaryReader(File.OpenRead(bin.txt) ))//工作正常



BinaryReader ll = new BinaryReader(bin.txt); //如果没有File.OpenRead就行不通,





StreamReader mm = new StreamReader(bin.txt); //这里工作没有File.OpenRead,

Why It Is Require To To Add File.OpenRead BinaryReader But Not In StreamReader ?

BinaryReader ll = new BinaryReader(File.OpenRead("bin.txt")) // Work's Fine

BinaryReader ll = new BinaryReader("bin.txt"); // Here It Does Not Work's Fine Without File.OpenRead ,


StreamReader mm = new StreamReader("bin.txt"); // Here It Work's Fine Without File.OpenRead ,

推荐答案

因为这是他们编码的方式,你需要向微软的开发人员询问为什么某些方法以某种方式编码。



使用BinaryReader它用于读取流,你必须记住流不必是一个文件,它可能是一个网络连接等.BioaryReader不关心数据来自何处,所以你必须先使用适当的机制打开流,然后将该流传递给BinaryReader进行读取。 br />


StreamReader以同样的方式工作,你可以将它传递给它来读取。但是它也接受一个文件名,并在内部打开该文件的流。



可能有一个很好的理由说BinaryReader也不支持这个,也许是谁写过BinaryReader并不认为允许文件名通过,而是那个人写道,StreamReader认为这样做很方便,谁知道呢。
Because that's how they've been coded, you'd need to ask the devs at Microsoft as to why certain methods are coded in a certain way.

With the BinaryReader it is intended for reading a stream, and you have to remember that a stream doesn't have to be a file, it could be a network connection etc. The BinaryReader doesn't concern itself with where the data is coming from so you have to use the appropriate mechanism to open the stream first, and pass that stream to the BinaryReader to read.

StreamReader works the same way, you can pass it the stream to read. However it also accepts a filename and will internally open the stream for that file.

There might be a good reason that BinaryReader doesn't support this also, maybe whoever wrote BinaryReader didn't think to allow a filename to passed, but the person who wrote StreamReader thought it would be a convenient thing to do, who knows.


这篇关于为什么需要添加File.OpenRead BinaryReader但不在StreamReader中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆