• C#FileStream复制大文件


    private void CopyFile(string fromFile, string toFile, int lengthEachTime)
            {
                FileStream fileToCopy = new FileStream(fromFile, FileMode.Open, FileAccess.Read);
                FileStream copyToFile = new FileStream(toFile, FileMode.Append, FileAccess.Write);
                int lengthToCopy;
                if (lengthEachTime < fileToCopy.Length)//如果分段拷贝,即每次拷贝内容小于文件总长度
                {
                    byte[] buffer = new byte[lengthEachTime];
                    int copied = 0;
                    while (copied <= ((int)fileToCopy.Length - lengthEachTime))//拷贝主体部分
                    {
                        lengthToCopy = fileToCopy.Read(buffer, 0, lengthEachTime);
                        fileToCopy.Flush();
                        copyToFile.Write(buffer, 0, lengthEachTime);
                        copyToFile.Flush();
                        copyToFile.Position = fileToCopy.Position;
                        copied += lengthToCopy;
                    }
                    int left = (int)fileToCopy.Length - copied;//拷贝剩余部分
                    lengthToCopy = fileToCopy.Read(buffer, 0, left);
                    fileToCopy.Flush();
                    copyToFile.Write(buffer, 0, left);
                    copyToFile.Flush();
                }
                else//如果整体拷贝,即每次拷贝内容大于文件总长度
                {
                    byte[] buffer = new byte[fileToCopy.Length];
                    fileToCopy.Read(buffer,0,(int)fileToCopy.Length);
                    fileToCopy.Flush();
                    copyToFile.Write(buffer, 0, (int)fileToCopy.Length);
                    copyToFile.Flush();
                }
                fileToCopy.Close();
                copyToFile.Close();
            }
  • 相关阅读:
    Linux的概念与体系
    Python快速教程
    Qt控件精讲一:按钮
    xml2-config not found
    Ubuntu 12.04更新源
    Adaboost的几个人脸检测网站
    关于matlab矩阵卷积conv2和傅里叶变换求卷积ifft2的关系
    char数组和String互转
    STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase
    《离散数学》-图论6.7
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1766630.html
Copyright © 2020-2023  润新知