1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Reflection;
5 using System.Text;
6
7 namespace IO目录管理
8 {
9 class Program
10 {
11 private string _StrSourcePath = @"C:\Users\MO\Desktop\1.txt"; //源文件目录
12 private string _StrTagrgetPath = @"C:\Users\MO\Desktop\2.txt"; //目标文件目录
13 public void Test()
14 {
15 //路径合法性判断
16 if (File.Exists(_StrSourcePath))
17 {
18 //构造读取文件流对象
19 using (FileStream fsRead = new FileStream(_StrSourcePath, FileMode.Open)) //打开文件,不能创建新的
20 {
21 //构建写文件流对象
22 using (FileStream fsWrite = new FileStream(_StrTagrgetPath, FileMode.Create)) //没有找到就创建
23 {
24 //开辟临时缓存内存
25 byte[] byteArrayRead = new byte[1024 * 1024]; // 1字节*1024 = 1k 1k*1024 = 1M内存
26
27 //通过死缓存去读文本中的内容
28 while (true)
29 {
30 //readCount 这个是保存真正读取到的字节数
31 int readCount = fsRead.Read(byteArrayRead, 0, byteArrayRead.Length);
32
33 //开始写入读取到缓存内存中的数据到目标文本文件中
34 fsWrite.Write(byteArrayRead, 0, readCount);
35
36
37 //既然是死循环 那么什么时候我们停止读取文本内容 我们知道文本最后一行的大小肯定是小于缓存内存大小的
38 if (readCount < byteArrayRead.Length)
39 {
40 break; //结束循环
41 }
42 }
43 }
44 }
45 }
46 else
47 {
48 Console.WriteLine("源路径或者目标路径不存在。");
49 }
50 }
51
52 static void Main(string[] args)
53 {
54 Program p = new Program();
55 p.Test();
56 Console.ReadKey();
57
58 }
59 }
60 }