C# System.IO.FileStream
为文件提供 Stream,既支持同步读写操作,也支持异步读写操作。
1 using System; 2 using System.IO; 3 using System.Text; 4 5 class Test 6 { 7 8 public static void Main() 9 { 10 string path = @"c:\temp\MyTest.txt"; 11 12 // Delete the file if it exists. 13 if (File.Exists(path)) 14 { 15 File.Delete(path); 16 } 17 18 //Create the file. 19 using (FileStream fs = File.Create(path)) 20 { 21 AddText(fs, "This is some text"); 22 AddText(fs, "This is some more text,"); 23 AddText(fs, "\r\nand this is on a new line"); 24 AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); 25 26 for (int i=1;i < 120;i++) 27 { 28 AddText(fs, Convert.ToChar(i).ToString()); 29 30 } 31 } 32 33 //Open the stream and read it back. 34 using (FileStream fs = File.OpenRead(path)) 35 { 36 byte[] b = new byte[1024]; 37 UTF8Encoding temp = new UTF8Encoding(true); 38 while (fs.Read(b,0,b.Length) > 0) 39 { 40 Console.WriteLine(temp.GetString(b)); 41 } 42 } 43 } 44 45 private static void AddText(FileStream fs, string value) 46 { 47 byte[] info = new UTF8Encoding(true).GetBytes(value); 48 fs.Write(info, 0, info.Length); 49 } 50 }
构造函数
| FileStream(IntPtr, FileAccess) |
使用指定的读/写权限为指定的文件句柄初始化 FileStream 类的新实例。 |
| FileStream(IntPtr, FileAccess, Boolean) |
使用指定的读/写权限和 FileStream 实例所属权为指定的文件句柄初始化 |
| FileStream(IntPtr, FileAccess, Boolean, Int32) |
使用指定的读/写权限、 |
| FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean) |
使用指定的读/写权限、 |
| FileStream(SafeFileHandle, FileAccess) |
使用指定的读/写权限为指定的文件句柄初始化 FileStream 类的新实例。 |
| FileStream(SafeFileHandle, FileAccess, Int32) |
使用指定的读/写权限和缓冲区大小为指定的文件句柄初始化 FileStream 类的新实例。 |
| FileStream(SafeFileHandle, FileAccess, Int32, Boolean) |
使用指定的读/写权限、缓冲区大小和同步或异步状态为指定的文件句柄初始化FileStream 类的新实例。 |
| FileStream(String, FileMode) |
使用指定的路径和创建模式初始化 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileAccess) |
使用指定的路径、创建模式和读/写权限初始化 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileAccess, FileShare) |
使用指定的路径、创建模式、读/写权限和共享权限创建 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileAccess, FileShare, Int32) |
用指定的路径、创建模式、读/写及共享权限和缓冲区大小初始化 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) |
使用指定的路径、创建模式、读/写和共享权限、缓冲区大小和同步或异步状态初始化 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) |
使用指定的路径、创建模式、读/写和共享权限、其他 FileStreams 可以具有的对此文件的访问权限、缓冲区大小和附加文件选项初始化 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions) |
使用指定的路径、创建模式、访问权限和共享权限、缓冲区大小和附加文件选项初始化 FileStream 类的新实例。 |
| FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity) |
使用指定的路径、创建模式、访问权限和共享权限、缓冲区大小、附加文件选项、访问控制和审核安全初始化 FileStream 类的新实例。 |
属性
| CanRead |
获取一个值,该值指示当前流是否支持读取。 |
| CanSeek |
获取一个值,该值指示当前流是否支持查找。 |
| CanTimeout |
获取一个值,该值确定当前流是否可以超时。 (Inherited from Stream) |
| CanWrite |
获取一个值,该值指示当前流是否支持写入。 |
| Handle |
获取当前 |
| IsAsync |
获取一个值,它指示 |
| Length |
获取用字节表示的流长度。 |
| Name |
获取 |
| Position |
获取或设置此流的当前位置。 |
| ReadTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取多长时间。 (Inherited from Stream) |
| SafeFileHandle |
获取 SafeFileHandle 对象,它代表当前 FileStream 对象所封装的文件的操作系统文件句柄。 |
| WriteTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试写入多长时间。 (Inherited from Stream) |
方法
| BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) |
开始异步读操作。 请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。 |
| BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) |
开始异步写操作。 请考虑改用 WriteAsync(Byte[], Int32, Int32, CancellationToken)。 |
| CopyTo(Stream) |
从当前流中读取字节并将其写入到另一流中。 (Inherited from Stream) |
| CopyTo(Stream, Int32) |
使用指定的缓冲区大小,从当前流中读取字节并将其写入到另一流中。 (Inherited from Stream) |
| CopyToAsync(Stream) |
从当前流中异步读取字节并将其写入到另一个流中。 (Inherited from Stream) |
| CopyToAsync(Stream, CancellationToken) | Inherited from Stream |
| CopyToAsync(Stream, Int32) |
使用指定的缓冲区大小,从当前流中异步读取字节并将其写入到另一流中。 (Inherited from Stream) |
| CopyToAsync(Stream, Int32, CancellationToken) |
使用指定的缓冲区大小和取消令牌,从当前流中异步读取字节并将其写入到另一个流中。 (Inherited from Stream) |
| CreateObjRef(Type) |
创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (Inherited from MarshalByRefObject) |
| CreateWaitHandle() |
分配 WaitHandle 对象。 (Inherited from Stream) |
| Dispose() |
释放由 Stream 使用的所有资源。 (Inherited from Stream) |
| Dispose(Boolean) |
释放由 FileStream 占用的非托管资源,还可以另外再释放托管资源。 |
| EndRead(IAsyncResult) |
等待挂起的异步读操作完成。 (请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。) |
| EndWrite(IAsyncResult) |
结束异步写入操作,在 I/O 操作完成之前一直阻止。 (请考虑改用WriteAsync(Byte[], Int32, Int32, CancellationToken)。) |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (Inherited from Object) |
| Finalize() |
确保垃圾回收器回收 |
| Flush() |
清除此流的缓冲区,使得所有缓冲数据都写入到文件中。 |
| Flush(Boolean) |
清除此流的缓冲区,将所有缓冲数据都写入到文件中,并且也清除所有中间文件缓冲区。 |
| FlushAsync() |
异步清除此流的所有缓冲区并导致所有缓冲数据都写入基础设备中。 (Inherited from Stream) |
| FlushAsync(CancellationToken) |
异步清理这个流的所有缓冲区,并使所有缓冲数据写入基础设备,并且监控取消请求。 |
| GetAccessControl() |
获取 FileSecurity 对象,该对象封装当前 FileStream 对象所描述的文件的访问控制列表 (ACL) 项。 |
| GetHashCode() |
作为默认哈希函数。 (Inherited from Object) |
| GetLifetimeService() |
检索控制此实例的生存期策略的当前生存期服务对象。 (Inherited from MarshalByRefObject) |
| GetType() |
获取当前实例的 Type。 (Inherited from Object) |
| InitializeLifetimeService() |
获取生存期服务对象来控制此实例的生存期策略。 (Inherited from MarshalByRefObject) |
| Lock(Int64, Int64) |
防止其他进程读取或写入 FileStream。 |
| MemberwiseClone() |
创建当前 Object 的浅表副本。 (Inherited from Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (Inherited from MarshalByRefObject) |
| ObjectInvariant() |
提供对 Contract 的支持。 (Inherited from Stream) |
| Read(Byte[], Int32, Int32) |
从流中读取字节块并将该数据写入给定缓冲区中。 |
| ReadAsync(Byte[], Int32, Int32) |
从当前流异步读取字节序列,并将流中的位置提升读取的字节数。 (Inherited from Stream) |
| ReadAsync(Byte[], Int32, Int32, CancellationToken) |
从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。 |
| ReadByte() |
从文件中读取一个字节,并将读取位置提升一个字节。 |
| Seek(Int64, SeekOrigin) |
将该流的当前位置设置为给定值。 |
| SetAccessControl(FileSecurity) |
将 FileSecurity 对象所描述的访问控制列表 (ACL) 项应用于当前 FileStream 对象所描述的文件。 |
| SetLength(Int64) |
将该流的长度设置为给定值。 |
| ToString() |
返回表示当前对象的字符串。 (Inherited from Object) |
| Unlock(Int64, Int64) |
允许其他进程访问以前锁定的某个文件的全部或部分。 |
| Write(Byte[], Int32, Int32) |
将字节块写入文件流。 |
| WriteAsync(Byte[], Int32, Int32) |
将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。 (Inherited from Stream) |
| WriteAsync(Byte[], Int32, Int32, CancellationToken) |
将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。 |
| WriteByte(Byte) |
一个字节写入文件流中的当前位置。 |
| IDisposable.Dispose() |
释放由 Stream 使用的所有资源。 (Inherited from Stream) |