C#基础-FileStream

一、FileStream的基础知识


  属性:
           CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取
           CanWrite 判断当前流是否支持写入,返回bool值,True表示可以写入


  方法:
           Read() 从流中读取数据,返回字节数组
           Write() 将字节块(字节数组)写入该流
           Seek() 设置文件读取或写入的起始位置
           Flush() 清除该流缓冲区,使得所有缓冲的数据都被写入到文件中
           Close() 关闭当前流并释放与之相关联的所有系统资源


  文件的访问方式:(FileAccess)
           FileAccess.Read(对文件读访问)
           FileAccess.Write(对文件进行写操作)
           FileAccess.ReadWrite(对文件读或写操作)


  文件打开模式:(FileMode)包括6个枚举
          FileMode.Append 打开现有文件准备向文件追加数据,只能同FileAccess.Write一起使用
          FileMode.Create 指示操作系统应创建新文件,如果文件已经存在,它将被覆盖
          FileMode.CreateNew 指示操作系统应创建新文件,如果文件已经存在,将引发异常
          FileMode.Open 指示操作系统应打开现有文件,打开的能力取决于FileAccess所指定的值
          FileMode.OpenOrCreate 指示操作系统应打开文件,如果文件不存在则创建新文件
          FileMode.Truncate 指示操作系统应打开现有文件,并且清空文件内容


  文件共享方式:(FileShare)
         FileShare方式是为了避免几个程序同时访问同一个文件会造成异常的情况。

  文件共享方式包括四个:
        FileShare.None 谢绝共享当前文件
        FileShare.Read 充许别的程序读取当前文件
        FileShare.Write 充许别的程序写当前文件
        FileShare.ReadWrite 充许别的程序读写当前文

二、FileStream的异步操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace ConsoleStream
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePaths = @"E:TestTestlocal1.msi";

            System.IO.FileInfo f = new FileInfo(@"E:TestTestserver1.msi");
            int fileLength = Convert.ToInt32(f.Length.ToString());

            ThreadPool.SetMaxThreads(100, 100);
            Console.WriteLine("Start");
            using (System.IO.FileStream stream = new System.IO.FileStream(filePaths, FileMode.Create, FileAccess.Write, FileShare.Write,2048, true))
            {
                for (int i = 0; i < fileLength; i+= 1024)
                {
                    Console.WriteLine(i);
                    if (i == 102400)
                    {
                        Console.WriteLine("Stop");
                        Thread.Sleep(1000);
                        Console.WriteLine("Restart");
                    }
                    if(i==102400+1024)
                    {
                        Console.WriteLine("Next");
                        Thread.Sleep(1000);
                    }
                    int length = (int)Math.Min(1024, fileLength - i);
                    var bytes = GetFile(i, length);
                    stream.Write(bytes, 0, length);
                    stream.BeginWrite(bytes, 0, length, new AsyncCallback(Callback), stream);
                }
                stream.Flush();
                stream.Close();
                stream.Dispose();
            }
            Console.ReadKey();
        }
        public static byte[] GetFile(int start, int length)
        {
            string filepath = @"E:TestTestserver1.msi";
            using (System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite,1024, true))
            {
                byte[] buffer = new byte[length];
                fs.Position = start;
                fs.BeginRead(buffer, 0, length, new AsyncCallback(Completed), fs);
                return buffer;
            }
        }

        static void Completed(IAsyncResult result)
        {
            FileStream fs = (FileStream)result.AsyncState;
            fs.EndRead(result);
fs.Close(); }
public static void Callback(IAsyncResult result) { FileStream stream = (FileStream)result.AsyncState; stream.EndWrite(result); } } }

1. 如果Callback里添一句:stream.Close(),肯定直接报错:无法访问已关闭的文件。因为是循环写文件,所以不能把流给关闭了。

2. 主程序里stream.Close()要写在stream.Flush()的后面,不然无法清除缓存,将缓存数据写入文件

原文地址:https://www.cnblogs.com/lovecsharp094/p/5721839.html