文件流

1、使用filestream读写数据

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace _06文件流
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14 
15             //使用FileStream来读取数据
16             FileStream fsRead = new FileStream(@"C:UsersSpringRainDesktop
ew.txt", FileMode.OpenOrCreate, FileAccess.Read);
17             byte[] buffer = new byte[1024 * 1024 * 5];
18             //3.8M  5M
19             //返回本次实际读取到的有效字节数
20             int r = fsRead.Read(buffer, 0, buffer.Length);
21             //将字节数组中每一个元素按照指定的编码格式解码成字符串
22             string s = Encoding.UTF8.GetString(buffer, 0, r);
23             //关闭流
24             fsRead.Close();
25             //释放流所占用的资源
26             fsRead.Dispose();
27             Console.WriteLine(s);
28             Console.ReadKey();
29 
30 
31 
32             //使用FileStream来写入数据
33             //using (FileStream fsWrite = new FileStream(@"C:UsersSpringRainDesktop
ew.txt", FileMode.OpenOrCreate, FileAccess.Write))
34             //{
35             //    string str = "看我游牧又把你覆盖掉";
36             //    byte[] buffer = Encoding.UTF8.GetBytes(str);
37             //    fsWrite.Write(buffer, 0, buffer.Length);
38             //}
39             //Console.WriteLine("写入OK");
40             //Console.ReadKey();
41         }
42     }
43 }
View Code

2、使用文件流来实现多媒体文件的复制

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 
 8 namespace _07使用文件流来实现多媒体文件的复制
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //思路:就是先将要复制的多媒体文件读取出来,然后再写入到你指定的位置
15             string source = @"C:UsersSpringRainDesktop1、复习.wmv";
16             string target = @"C:UsersSpringRainDesktop
ew.wmv";
17             CopyFile(source, target);
18             Console.WriteLine("复制成功");
19             Console.ReadKey();
20         }
21 
22         public static void CopyFile(string soucre, string target)
23         {
24             //1、我们创建一个负责读取的流
25             using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
26             {
27                 //2、创建一个负责写入的流
28                 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
29                 {
30                     byte[] buffer = new byte[1024 * 1024 * 5];
31                     //因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取
32                     while (true)
33                     {
34                         //返回本次读取实际读取到的字节数
35                         int r = fsRead.Read(buffer, 0, buffer.Length);
36                         //如果返回一个0,也就意味什么都没有读取到,读取完了
37                         if (r == 0)
38                         {
39                             break;
40                         }
41                         fsWrite.Write(buffer, 0, r);
42                     }
43 
44                  
45                 }
46             }
47         }
48 
49 
50     }
51 }
View Code

3、StreamReader和StreamWriter

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 namespace _08StreamReader和StreamWriter
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //使用StreamReader来读取一个文本文件
14             //using (StreamReader sr = new StreamReader(@"C:UsersSpringRainDesktop抽象类特点.txt",Encoding.Default))
15             //{
16             //    while (!sr.EndOfStream)
17             //    {
18             //        Console.WriteLine(sr.ReadLine());
19             //    }
20             //}
21 
22 
23             //使用StreamWriter来写入一个文本文件
24             using (StreamWriter sw = new StreamWriter(@"C:UsersSpringRainDesktop
ewnew.txt",true))
25             {
26                 sw.Write("看我有木有把你覆盖掉");
27             }
28             Console.WriteLine("OK");
29             Console.ReadKey();
30         }
31     }
32 }
View Code


 

原文地址:https://www.cnblogs.com/liuslayer/p/4475593.html