首页 > 其他 > 详细

文件流

时间:2014-05-22 16:45:42      阅读:386      评论:0      收藏:0      [点我收藏+]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 文件流的操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 用于大文件的拷贝
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{

//快速得到文件流的三个方法
// FileStream fs = File.Open("path",FileMode.Open);
// FileStream fs1 = File.OpenRead("path");//返回一个读的文件流
// FileStream fs2 = File.OpenWrite("path");//返回一个写的文件流
//1准备一个用于读数据的文件流
using (FileStream streamReader = new FileStream(@"F:\1.txt",FileMode.Open))//using的本质是一个try finally
{
//2.准备一个用于写数据的文件流
using (FileStream streamWrite = new FileStream(@"F:\2.txt", FileMode.Create))
{
//3.准备一个字节数组用于保存读出来的数据
byte[] data = new byte[1024 * 1024];
//4.用读的文件流将数据读出来,放到字节数组里
int lenght = 0;
do{
//返回实际读取的大小
lenght = streamReader.Read(data, 0, data.Length);//读取的时候是从上一次读到的地方继续读取,而不是从头开始读可以用streamReader.Position进行查看

streamWrite.Write(data,0,lenght);
}while(lenght>=data .Length);
}

}

MessageBox.Show("文件拷贝成功!");
}
}
}

文件流,布布扣,bubuko.com

文件流

原文:http://www.cnblogs.com/sumg/p/3743980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!