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时,第三个参数的意思<最多读取的字节数>,不是数组的长度是要读取多少字节

转载于:https://www.cnblogs.com/cuijl/p/4553734.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值