名稱
|
說明
|
使用默認的緩衝區大小 4096 字節初始化 BufferedStream 類的新實例。
|
|
使用指定的緩衝區大小初始化 BufferedStream 類的新實例。
|
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.IO;
namespace
FileOptionApplication
{
public partial class Form16 : Form
{
public Form16()
{
InitializeComponent();
}
/// <summary>
/// 打開原始文件
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "文本文件(*.txt)|*.txt";
if (openfile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openfile.FileName.ToString();
}
}
/// <summary>
/// 備份目標文件;Stream 和 BufferedStream 的實例
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
string targetpath = @"c:\" + textBox2.Text + ".txt";
FileStream fs =File.Create(targetpath);
fs.Dispose();
fs.Close();
string sourcepath = textBox1.Text;
Stream outputStream= File.OpenWrite(targetpath);
Stream inputStream = File.OpenRead(sourcepath);
BufferedStream bufferedInput = new BufferedStream(inputStream);
BufferedStream bufferedOutput = new BufferedStream(outputStream);
byte[] buffer = new Byte[4096];
int bytesRead;
while ((bytesRead =bufferedInput.Read(buffer, 0,4096)) > 0)
{
bufferedOutput.Write(buffer, 0, bytesRead);
}
//經過緩衝區進行讀寫
MessageBox.Show("給定備份的文件已建立", "提示");
bufferedOutput.Flush();
bufferedInput.Close();
bufferedOutput.Close();
//刷新並關閉 BufferStream
}
}
}
|