在 c# 语言中文件读写流使用 filestream 类来表示,filestream 类主要用于文件的读写,不仅能读写普通的文本文件,还可以读取图像文件、声音文件等不同格式的文件。区别于file类的是它对文件可进行分步读写,减小内存压力,缺点是我们需要手动的关闭和释放资源,
fileaccess 枚举类型主要用于设置文件的访问方式,具体的枚举值如下。
filemode 枚举类型主要用于设置文件打开或创建的方式,具体的枚举值如下。
fileshare 枚举类型主要用于设置多个对象同时访问同一个文件时的访问控制,具体的枚举值如下。
fileoptions 枚举类型用于设置文件的高级选项,包括文件是否加密、访问后是否删除等,具体的枚举值如下。
filestream 类的构造方法有很多,这里介绍一些常用的构造方法,如下表所示。
构造方法 | 说明 |
---|---|
filestream(string path, filemode mode) | 使用指定路径的文件、文件模式创建 filestream 类的实例 |
filestream(string path, filemode mode, fileaccess access) | 使用指定路径的文件、文件打开模式、文件访问模式创建 filestream 类的实例 |
filestream(string path, filemode mode, fileaccess access, fileshare share) | 使用指定的路径、创建模式、读写权限和共享权限创建 filestream 类的一个新实例 |
filestream(string path, filemode mode, fileaccess access, fileshare share, int buffersize, fileoptions options) | 使用指定的路径、创建模式、读写权限和共享权限、其他 文件选项创建 filestream 类的实例 |
属性或方法 | 作用 |
---|---|
bool canread | 只读属性,获取一个值,该值指示当前流是否支持读取 |
bool canseek | 只读属性,获取一个值,该值指示当前流是否支持查找 |
bool canwrite | 只读属性,获取一个值,该值指示当前流是否支持写入 |
bool isasync | 只读属性,获取一个值,该值指示 filestream 是异步还 是同步打开的 |
long length | 只读属性,获取用字节表示的流长度 |
string name | 只读属性,获取传递给构造方法的 filestream 的名称 |
long position | 属性,获取或设置此流的当前位置 |
int read(byte[] array, int offset, int count) | 从流中读取字节块并将该数据写入给定缓冲区中 |
int readbyte() | 从文件中读取一个字节,并将读取位置提升一个字节 |
long seek(lorig offset, seekorigin origin) | 将该流的当前位置设置为给定值 |
void lock(long position, long length) | 防止其他进程读取或写入 system.io.filestream |
void unlock(long position, long length) | 允许其他进程访问以前锁定的某个文件的全部或部分 |
void write(byte[] array, int offset, int count) | 将字节块写入文件流 |
void writebyte(byte value) | 将一个字节写入文件流中的当前位置 |
file是一个静态类;filestream是一个非静态类。
file:是一个文件的类,对文件进行操作。其内部封装了对文件的各种操作(msdn:提供用于创建、复制、删除、移动和打开单一文件的静态方法,并协助创建filestream对象)。
filestream:文件流的类。对txt,xml,avi等文件进行内容写入、读取、复制...时候需要使用的一个工具。
string path = "d:\\test.txt"; filestream filestream1 = **new** filestream(path, filemode.open); filestream filestream2 = **new** filestream(path, filemode.open, fileaccess.read); filestream filestream3 = **new** filestream(path, filemode.open, fileaccess.readwrite, fileshare.read); filestream filestream4 = **new** filestream(path, filemode.open, fileaccess.read, fileshare.read, 10, fileoptions.none);
//定义文件路径 string path = @"d:\\code\\test.txt"; //创建 filestream 类的实例 filestream filestream = new filestream(path, filemode.openorcreate, fileaccess.readwrite, fileshare.readwrite); //定义属性 string msg = "11111"; //将字符串转换为字节数组 byte[] bytes = encoding.utf8.getbytes(msg); //向文件中写入字节数组 filestream.write(bytes, 0, bytes.length); //刷新缓冲区 filestream.flush(); //关闭流 filestream.close();
class program { static void main(string[] args) { //定义文件路径 string path = @"d:\\code\\test.txt"; //判断是否含有指定文件 if (file.exists(path)) { filestream filestream = new filestream(path, filemode.open, fileaccess.read); //定义存放文件信息的字节数组 byte[] bytes = new byte[filestream.length]; //读取文件信息 filestream.read(bytes, 0, bytes.length); //将得到的字节型数组重写编码为字符型数组 char[] c = encoding.utf8.getchars(bytes); //输出 console.writeline(c); //关闭流 filestream.close(); } else { console.writeline("不存在!"); } } }
public class filesreamtest { public static void copyfile(string source,string target) { //读取文件流 using (filestream fsread = new filestream(source,filemode.open,fileaccess.read)) { //写入流 using (filestream fswrite = new filestream(target,filemode.openorcreate,fileaccess.write)) { byte[] buffer = new byte[1024*1024*5]; while (true) { //返回读取字节数 int r= fsread.read(buffer,0,buffer.length); if (r==0) { break; } //写入 fswrite.write(buffer,0,r); } } } } } static void main(string[] args) { string source =@"e:\功能流程.7z"; string target=@"c:\users\ch190006\desktop\test\功能流程.7z"; filesreamtest.copyfile(source,target); console.readkey(); }
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论