http://blog.csdn.net/feliciafay/article/details/6157356

http://blog.csdn.net/feliciafay/article/details/6157353

□简介StreamReader
以Stream为服务中心。那么这个stream一定是文件的stream么?不一定,可能是文件的,也可能是其它的,比如从HttpWebResponse转化而来的Stream。

构造函数的两大类
1从stream中读取
StreamReader(Stream) Initializes a new instance of the StreamReader class for the specified stream.

2从File中读取
StreamReader(String) Initializes a new instance of the StreamReader class for the specified file name.

成员函数
1. Peek() 
Returns the next available character but does not consume it. 
2. Read() 
Reads the next character from the input stream and advances the character position by one character. 
3. Read(Char[], Int32, Int32) 
Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index. 
4. ReadBlock() 
Reads a maximum of count characters from the current stream, and writes the data to buffer, beginning at index. 
5. ReadLine() 
Reads a line of characters from the current stream and returns the data as a string. 
6. ReadToEnd() 
Reads the stream from the current position to the end of the stream.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading

lines of information from a standard text file.

□简介 FileStream 
是以文件为服务中心的。Exposes a Stream around a file

□FileStream的write()
是把byte[]写入到Stream中
public override void Write(byte[] array,int offset,int count)
array
 Type: System.Byte[]
 The buffer containing data to write to the stream.
offset是源头的偏移量
 Type: System.Int32
 The zero-based byte offset in array at which to begin copying bytes to the current stream. 
count
 Type: System.Int32
 The number of bytes to be written to the current stream
 return value 无返回值

□例 FileStream的read()
是把Stream读入到byte[]数组中
public override int Read(byte[] array,int offset,int count)
array
 Type: System.Byte[]
 When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current

source. 
offset 是目标的偏移量
 Type: System.Int32
 The byte offset in array at which the read bytes will be placed. 
count
 Type: System.Int32
 The maximum number of bytes to read. 
Return Value
 Type: System.Int32
 The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or

zero if the end of the stream is reached.
小结:
不管是FileStream的read()还是write()操作,offset这个参数永远只是指byte[]数组中的偏移量。具体地说,在read()中,offset指的是目标的偏移量,在write()中,offset指的是源的偏移量。

例 
FileStream fsSource = new FileStream(pathSource,FileMode.Open, FileAccess.Read)
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
 // 注意这里的anything这个说法很重要:Read may return anything from 0 to numBytesToRead返回的字节数可能为任意比numBytesToRead小的数。
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}

□还有哪些Stream?
BufferedStream

FileStream

MemoryStream

UnmanagedMemoryStream

Stream

StreamReader

StreamWriter

□还有哪些reader和writer?
BinaryReader 

Reads primitive data types as binary values in a specific encoding.
BinaryWriter 

Writes primitive types in binary to a stream and supports writing strings in a specific

encoding.

TextReader

Represents a reader that can read a sequential series of characters.
TextWriter

Represents a writer that can write a sequential series of characters. This class is abstract.
StringReader

Implements a TextReader that reads from a string.
StringWriter

Implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
StreamReader

Implements a TextReader that reads characters from a byte stream in a particular encoding.
StreamWriter

Implements a TextWriter for writing characters to a stream in a particular encoding.


1.StreamReader的read()
StreamReader的read()把Stream读入到char[]中。
public override int Read(char[] buffer,int index,int count)
buffer
 Type: System.Char[]
 When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the

current source. 
index 是目标数组的偏移量
 Type: System.Int32
 The index of buffer at which to begin writing. 
count
 Type: System.Int32
 The maximum number of characters to read. 
Return Value
 Type: System.Int32
 The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the count

parameter, depending on whether the data is available within the stream.

2.FileStream的read()
FileStream的read()把Stream读入到byte[]数组中
public override int Read(byte[] array,int offset,int count)
array
 Type: System.Byte[]
 When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current

source. 
offset 是目标数组的偏移量
 Type: System.Int32
 The byte offset in array at which the read bytes will be placed. 
count
 Type: System.Int32
 The maximum number of bytes to read. 
Return Value
 Type: System.Int32
 The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or

zero if the end of the stream is reached.

小结:基本相似,但是目标数组的类型不同,一个为char[],一个为byte[]。

stream~filestream的更多相关文章

  1. C#基础-FileStream

    一.FileStream的基础知识 属性:          CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取          CanWrite 判断当前流是否支持写入, ...

  2. MemoryStream和FileStream

    一,FileStream对象的数据来自文件,而MemoryStream对象的数据来自内存缓冲区.这两个类都继承自Stream类. 二,抽象基类System.IO.Stream代表流,它提供Read和W ...

  3. .NET Core装饰模式和.NET Core的Stream

    该文章综合了几本书的内容. 某咖啡店项目的解决方案 某咖啡店供应咖啡, 客户买咖啡的时候可以添加若干调味料, 最后要求算出总价钱. Beverage是所有咖啡饮料的抽象类, 里面的cost方法是抽象的 ...

  4. .NET Core/.NET之Stream简介

    之前写了一篇C#装饰模式的文章提到了.NET Core的Stream, 所以这里尽量把Stream介绍全点. (都是书上的内容) .NET Core/.NET的Streams 首先需要知道, Syst ...

  5. c#Stream学习笔记

    C# 温故而知新:Stream篇(—) http://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html 基本概念重点看这一篇. 什么 ...

  6. Stream(流)的基本操作

    //把流转化为文件  public static void StreamToFile(Stream stream, string filepath)        {            byte[ ...

  7. 【.Net】Byte,Stream,File的转换

    引言      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类     public static class FileHelper { / ...

  8. .NET Core/.NET之Stream简介 Rx.NET 简介

    .NET Core/.NET之Stream简介   之前写了一篇C#装饰模式的文章提到了.NET Core的Stream, 所以这里尽量把Stream介绍全点. (都是书上的内容) .NET Core ...

  9. 使用C# (.NET Core) 实现装饰模式 (Decorator Pattern) 并介绍 .NET/Core的Stream

    该文章综合了几本书的内容. 某咖啡店项目的解决方案 某咖啡店供应咖啡, 客户买咖啡的时候可以添加若干调味料, 最后要求算出总价钱. Beverage是所有咖啡饮料的抽象类, 里面的cost方法是抽象的 ...

随机推荐

  1. 一个简单的网页读字符串 SpeechLib

    //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib 前面设置内容引用别人 ...

  2. C++之------回调函数

    一:What?(什么是回调函数) 回调函数图文讲解 谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数. 例如Win32 ...

  3. 二维码识别:Halcon与C++中多字节环境下的字节编码格式设置和转换

    Halcon环境下可通过设置set_system(‘filename_encoding’, ‘utf8’),可以将二维码的识别结果解析出汉字. VS环境下则需要将utf8转换成gbk格式.代码如下: ...

  4. 手机三种SIM卡 你所不知道的剪卡“秘密”

    SIM卡物理尺寸的发展是逐渐轻薄化,尺寸逐渐缩小的一个过程,最早手机中的卡都是2FF,2003年国际标准提出3FF,当前很多终端都使用这种形态的卡,4FF在2011年的国际标准会议中提出,2012年纳 ...

  5. perl 爬取csdn

    <pre name="code" class="python">use LWP::UserAgent; use POSIX; use HTML::T ...

  6. 数据可视化的优秀入门书籍有哪些,D3.js 学习资源汇总

    习·D3.js 学习资源汇总 除了D3.js自身以外,许多可视化工具包都是基于D3开发的,所以对D3的学习就显得很重要了,当然如果已经有了Javascript的经验,学起来也会不费力些. Github ...

  7. Linux系统编程(7)—— 进程之进程概述

    我们知道,每个进程在内核中都有一个进程控制块(PCB)来维护进程相关的信息,Linux内核的进程控制块是task_struct结构体.现在我们全面了解一下其中都有哪些信息. 进程id.系统中每个进程有 ...

  8. SDP简要解析

    1.概述SDP也是MMUSIC工作组的一个产品,在MBONE内容中用得很多.其目的就是在媒体会话中,传递媒体流信息,允许会话描述的接收者去参与会话.SDP基本上在internet上工作.他定义了会话画 ...

  9. 【转】HDMI控制与组态剖析

    HDMI能够称霸为王者之尊吗?目前消费者陷入的困境就是Audio/Video的连接线数量过度庞大,而HDMI最大卖点之一就是可使用单一的连接线完全取代众多的影音连接线,简洁又方便.缺点是技术版本变动太 ...

  10. zoj 3471 Most Powerful(状态压缩dp)

    Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...