FileStream读取文件 案例(复制)

 1              // FileStream针对操作"字节"
 2             //StreamReader 和 StreamWriter是针对"字符"进行操作的
 3 
 4 
 5             string str = string.Empty;
 6             using (FileStream ss = new FileStream(@"C:\Users\Administrator\Desktop\a.txt", FileMode.OpenOrCreate, FileAccess.Read))
 7             {//PC不会自己进行释放,需要借助using
 8                 byte[] bb = new byte[1024 * 1024 * 5];//缓冲区的大小
 9                 int a = ss.Read(bb, 0, bb.Length);//实际写入缓冲区的大小
10                 str = Encoding.Default.GetString(bb, 0, a);//进行读取
11             }
12             Console.WriteLine(str);
13             Console.ReadKey();

 

 

案例图片(winform)

 

 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.IO;
11 
12 namespace WindowsFormsApplication6
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             OpenFileDialog ofd = new OpenFileDialog();
24             ofd.Title = "请选择要复制的文件";
25             ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
26             ofd.Filter = "所有文件|*.*";
27             ofd.ShowDialog();
28             textBox1.Text = ofd.FileName;
29         }
30 
31         private void button2_Click(object sender, EventArgs e)
32         {
33             SaveFileDialog s = new SaveFileDialog();
34             s.Title = "请选择要保存文件的路径";
35             s.InitialDirectory = @"C:\Users\Administrator\Desktop";
36             s.ShowDialog();
37             textBox2.Text = s.FileName;//获取最终保存的文件路径和名称
38             using (FileStream fread = new FileStream(textBox1.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Read))
39             {//先读取,再写出,此处为读取
40                 using (FileStream fwrite = new FileStream(textBox2.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Write))
41                 {//此处为写出
42 
43                     //设置进度条的最大值
44                     progressBar1.Maximum = (int)fread.Length;
45                     byte[] bb = new byte[1024 * 1024 * 5];//设定缓冲池的大小
46                     while (true)
47                     {
48                         //按照缓冲池的大小读取文件内容,a为实际读取的大小
49                         int a = fread.Read(bb, 0, bb.Length);
50                         if (a == 0)
51                         {
52                             break;
53                         }
54                         //根据缓冲池读取的实际大小来输出
55                         fwrite.Write(bb,0,a);
56                         //设置进度条的进度
57                         progressBar1.Value = (int)fread.Length;
58                     }
59                     MessageBox.Show("复制成功");
60                 }
61             }
62 
63 
64 
65          
66 
67         }
68     }
69 }

 

posted @ 2017-02-13 14:17  冲天小肥牛  阅读(574)  评论(0编辑  收藏  举报