StreamReader和StreamWriter已经可以实现文件读写,为什么还要使用FileStream? StreamReader和StreamWriter已经可以实现文件读写,为什么还要使用FileStream? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、StreamReader只用来读字符串。2、StreamReader可以用来读任何Stream,包括FileStream也包括NetworkStream,MemoryStream等等。3、FileStream用来操作文件流。可读写,可字符串,也可二进制。重要的区别是,StreamReader是读者,用来读任何输入流;FileStream是文件流,可以被读,被写。 比如说看看 StreamWriter源代码public StreamWriter(string path) : this(path, false, StreamWriter.UTF8NoBOM, 1024){}public StreamWriter(string path, bool append, Encoding encoding, int bufferSize) : this(path, append, encoding, bufferSize, true){}internal StreamWriter(string path, bool append, Encoding encoding, int bufferSize, bool checkHost) : base(null){ if (path == null) { throw new ArgumentNullException("path"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (path.Length == 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath")); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum")); } Stream streamArg = StreamWriter.CreateFile(path, append, checkHost); this.Init(streamArg, encoding, bufferSize, false);}private static Stream CreateFile(string path, bool append, bool checkHost){ FileMode mode = append ? FileMode.Append : FileMode.Create; return new FileStream(path, mode, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost);}[SecuritySafeCritical]private void Init(Stream streamArg, Encoding encodingArg, int bufferSize, bool shouldLeaveOpen){ this.stream = streamArg; this.encoding = encodingArg; this.encoder = this.encoding.GetEncoder(); if (bufferSize < 128) { bufferSize = 128; } this.charBuffer = new char[bufferSize]; this.byteBuffer = new byte[this.encoding.GetMaxByteCount(bufferSize)]; this.charLen = bufferSize; if (this.stream.CanSeek && this.stream.Position > 0L) { this.haveWrittenPreamble = true; } this.closable = !shouldLeaveOpen; if (Mda.StreamWriterBufferedDataLost.Enabled) { string cs = null; if (Mda.StreamWriterBufferedDataLost.CaptureAllocatedCallStack) { cs = Environment.GetStackTrace(null, false); } this.mdaHelper = new StreamWriter.MdaHelper(this, cs); }}这就是原因!所谓原因,你要自己看。这并不是对你的过高要求,这个要求是正合适那些想在工作前就学到本领的人的。 这好比如说,苹果出了个IPhone,它不能强迫人家富士康不再给别的厂商代工代工电子产品吧。 c#中文件输入输出问题? C#自定义控件问题_ C# GIF动画各帧透明的处理 UdpClient如果传输文件? 如何将一个数据库的两个表的数据在一个DataGrid控件上显示????急呀!! C#中 static & extern 一起用是怎么个解释? wpf 我在vm里有个List属性.怎么绑定? 如何让用户输入的字符匹配以下格式(日期格式)yyyy-mm-dd hh:mm:ss 大家能不能用一个实际的例子说明一下应用域存在的用处 高手进来帮帮忙:) 客户端发送和接受消息在一个函数里怎么写 vs2008+sql2005的安装问题
2、StreamReader可以用来读任何Stream,包括FileStream也包括NetworkStream,MemoryStream等等。3、FileStream用来操作文件流。可读写,可字符串,也可二进制。重要的区别是,StreamReader是读者,用来读任何输入流;FileStream是文件流,可以被读,被写。
{
}public StreamWriter(string path, bool append, Encoding encoding, int bufferSize) : this(path, append, encoding, bufferSize, true)
{
}internal StreamWriter(string path, bool append, Encoding encoding, int bufferSize, bool checkHost) : base(null)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (path.Length == 0)
{
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
}
if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
}
Stream streamArg = StreamWriter.CreateFile(path, append, checkHost);
this.Init(streamArg, encoding, bufferSize, false);
}private static Stream CreateFile(string path, bool append, bool checkHost)
{
FileMode mode = append ? FileMode.Append : FileMode.Create;
return new FileStream(path, mode, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost);
}[SecuritySafeCritical]
private void Init(Stream streamArg, Encoding encodingArg, int bufferSize, bool shouldLeaveOpen)
{
this.stream = streamArg;
this.encoding = encodingArg;
this.encoder = this.encoding.GetEncoder();
if (bufferSize < 128)
{
bufferSize = 128;
}
this.charBuffer = new char[bufferSize];
this.byteBuffer = new byte[this.encoding.GetMaxByteCount(bufferSize)];
this.charLen = bufferSize;
if (this.stream.CanSeek && this.stream.Position > 0L)
{
this.haveWrittenPreamble = true;
}
this.closable = !shouldLeaveOpen;
if (Mda.StreamWriterBufferedDataLost.Enabled)
{
string cs = null;
if (Mda.StreamWriterBufferedDataLost.CaptureAllocatedCallStack)
{
cs = Environment.GetStackTrace(null, false);
}
this.mdaHelper = new StreamWriter.MdaHelper(this, cs);
}
}
这就是原因!所谓原因,你要自己看。这并不是对你的过高要求,这个要求是正合适那些想在工作前就学到本领的人的。