FileStream使用小记

流用于对IO处理 

在System.IO名称空间中有以下类 

BinaryReader/Writer 

TextReader/Writer 

Stream 

其中类Stream为抽象类。由此有三个派生类: 

MemoryStream:对内存进行读取与写入 

BufferedStream:对缓冲器进行读取/写入 

FileStream:对文件执行读取与写入 

TextReader/Writer为抽象类。由此派生类: 

StreamReader/StreamWirter 

StringReader/StringWriter 

文件流Write使用:

String path=@"D:\aa.Text";
using(FileStream fs=new FileStream(path,FileModel.Open,FileAccess.ReadWrite)){
  byte[] body=new byte[fs.Length];
  fs.Read(body,0,(int)body.Length);
  byte[] head=new byte[44];
  fs.Write(head,0,(int)head.Length);
  fs.Write(body,0,(int)body.Length);
}

这块在拼接write时,第三个参数的意思<要写入当前流的长度>,就是说当前数组的长度而不是写到流的长度。

文件流Read使用:

1 String path=@"D:\aa.Text";
2 byte[] all=null;
3 using(FileStream fs=new FileStream(path,FileModel.Open,FileAccess.Read)){
4    all=new byte[44+path.Length];
5     //先在Byte[]中写入44个字节
6    fs.Read(all,44,(int)fs.Length);
7 }

这块在Read时,第三个参数的意思<最多读取的字节数>,不是数组的长度是要读取多少字节

posted @ 2015-06-05 09:05  大胖儿在努力  阅读(209)  评论(0编辑  收藏  举报