FileStream使用小记

时间:2023-03-09 01:46:52
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,,(int)body.Length);
byte[] head=new byte[];
fs.Write(head,,(int)head.Length);
fs.Write(body,,(int)body.Length);
}

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

文件流Read使用:

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

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