C# FileStream分块读取和保存文件

1 FileStream分块读取文件

图片 1图片 2

        public byte[] GetFileData(string fileName, long startPosition, long length)

        {

            byte[] data;

            using (FileStream fileRead = new FileStream(fileName, FileMode.Open, FileAccess.Read))

            {

                 data = new byte[length];

                fileRead.Seek(startPosition, SeekOrigin.Begin);

                fileRead.Read(data, 0, length);

                fileRead.Close();

                fileRead.Dispose();

            }
       }

View Code

2  FileStream追加数据到文件

图片 3图片 4

         public void AppendDataToFile(string fileName, byte[] data)

        {

            using (FileStream fileSave1 = new FileStream(fileName,

                FileMode.Append, FileAccess.Write) )

            {

                fileSave1.Write(data, 0, data.Length);

                fileSave1.Flush();

                fileSave1.Close();

                fileSave1.Dispose();

            }

        }

View Code

 

相关文章