stream~filestream

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,布布扣,bubuko.com

时间: 2024-10-26 18:55:07

stream~filestream的相关文章

C#基础-FileStream

一.FileStream的基础知识 属性:          CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取          CanWrite 判断当前流是否支持写入,返回bool值,True表示可以写入 方法:          Read() 从流中读取数据,返回字节数组          Write() 将字节块(字节数组)写入该流          Seek() 设置文件读取或写入的起始位置          Flush() 清除该流缓冲区,使得所有缓冲的数

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

引言      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类     public static class FileHelper { /// <summary> ///Stream转化成byte数组 /// </summary> /// <param name="stream"></param> /// <returns></returns> publ

用ueditor上传图片、文件等到七牛云存储

ueditor上传文件,是用数据流的形式上传的. 而七牛云存储官方文档中,只提供了文件路径上传的方式. 但是,仅仅是在官方文档中写了这一种方式. 事实上,利用VS的对象管理器,打开Qiniu的dll,我们可以看到以下东西: 其实Qiniu提供的SDK中,是可以利用文件流上传文件的. 所以,根据官方文档提供的案例,我们可以将上传改写成下面的样子: /// <summary> /// 上传文件 /// </summary> /// <param name="key&qu

下载大文件时OutofMemoryException

有一个大文件下载的问题,这两天查了一下,主要原因有两个: 发送的Get请求中,没有设置HttpCompletionOption这个参数,当下载大文件的时候,会等到response body中的数据全部加载完才开始下载.现在已经改成了HttpCompletionOption.ResponseHeadersRead,这样只要head加载完就可以开始下载 下载的时候使用FileStreamResult File(Stream fileStream, string contentType, string

C#综合揭秘——细说多线程(下)

引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I/O线程的开发,并行操作PLINQ等多个方面介绍多线程的开发.其中委托的BeginInvoke方法以及回调函数最为常用.而 I/O线程可能容易遭到大家的忽略,其实在开发多线程系统,更应该多留意I/O线程的操作.特别是在ASP.NET开发当中,可能更多人只会留意在客户端使用Ajax或者在服务器端使用UpdatePanel.其实合理使用I/O线程在通讯项目或文件下载时,能尽量降低IIS的压力.并行编程是Framework4.0中极力推广的

【UWP】不通过异常判断文件是否存在

从WP升到WinRT(Win8/WP8.1/UWP)后所有的文件操作都变成StorageFile和StorageFolder的方式,但是微软并没有提供判断文件是否存在的方法通常的做法我们可以通过下面方式判断文件是否存在 1.通过FileNotFoundException异常判断 public async Task<bool> isFilePresent(string fileName) { bool fileExists = true; Stream fileStream = null; St

基于C#百度图片批量下载工具的实现

在家没网,无聊怎么办?不如来看点美女图片吧,网络快时批量下载,有空时慢慢看,嘿嘿,本人是个好人.于是这个工具的实现,那简直是迫在眉睫啊,来看看是怎么实现的吧. 先上图片吧: 这是软件的WinForm界面,基于C#实现. 上代码,也就100多行. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Sys

Windows Phone 8.1 应用生命周期

一."后退键"不会终止应用 关于 Windows Phone 8.1 的应用生命周期,第一个要知道的关键就是:"后退键"不会终止应用! 在 8.0 时代,不断的按下"后退键"就可以完全的关闭并且终止应用,但在 8.1 中,这样的行为只会让应用处在 Suspended(挂起)状态,可以通过长按"后退键"进入多任务界面查看. 那如果还想像 8.0 一样终止应用呢?(虽然不推荐也没有必要)可以在多任务界面点击应用右上角的"

网站性能优化:动态缩略图技术实现思路

在网站开发过程中,大家都是如何解决多尺寸图片缩略图问题的呢?犹为典型的是电商网站,据了解,淘宝的图片缩略图是直接存储多张缩略图的方式,以满足各种情况下使用,因为它有牛逼的开源+自主开发的海量图片存储架构作支撑.但是,我们在做网站时,并不可能直接搬牛逼的架构过来,就可以达到预期的效果,况且各种成本投入也是有限的.所以一般性能优化的原则大都是这样:先考虑软件的优化,再考虑硬件的升级,当然土豪客户则除外. 很多网站可能没有对图片进行缩略图处理,上传时图片可能几百KB,在页面也是直接加载几百KB的图片大