IO操作

 I/O即input/output(输入和输出)的首字母缩写,顾名思义是程序对数据的输入和输出操作。输入指的是通过外接设备、文件向程序写入内容,输出是程序向外界设备输出内容,参照物是程序本身。IO中通过File类创建文件、复制、删除文件等,通过Directory创建文件夹,打印文件夹中文件名等。IO中通过流来读写数据,常见的流有:一.FileStream(是一个双向流,既可以读也可以写)、二.StreamWriter(进行字符操作、单向流---输出)(有以下几种常见参数类型:1(Stream stream)传入Stream流对象。2(string path)路径。3(string path,bool append)路径,是否追加(true  or false)。4(string path,bool append,Encoding.UTF8)路径,是否追加(true  or false),编码格式。)、StreamReader(进行字符操作、单向流---输入)、三.BinaryReader(进行字节操作)、BinaryWriter(进行字节操作)、四.BufferedStream。关系如下:

这四类流对数据的读写操作的具体事例有:

一.FileStream有关操作

View Code
  1 //FileStream创建文件并向文件中写入内容
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.IO;
  7 
  8 namespace ConsoleApplication1
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             //创建FileStream对象来创建或打开文件
 15             FileStream fs = File.Open(@"d:\1.cs", FileMode.OpenOrCreate);
 16             StringBuilder sb = new StringBuilder();
 17             //用StringBuilder对象获取要写入的内容块
 18             sb.Append(@"using System;
 19 using System.Collections.Generic;
 20 using System.Linq;
 21 using System.Text;
 22 using System.IO;
 23 
 24 namespace ConsoleApplication1
 25 {
 26     class Program
 27     {
 28         static void Main(string[] args)
 29         {
 30             //创建文件
 31             File.Create(@""d:\1.cs"");
 32             /*---向文件中写入内容---*/
 33             //创建FileStream对象来打开已创建文件
 34             FileStream fs = File.Open(@""d:\1.cs"", FileMode.Open);
 35             StringBuilder sb = new StringBuilder();
 36             sb.Append(@""追加内容"");
 37             byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString());
 38             fs.Write(buffer, 0, buffer.Length);
 39             fs.Close();
 40         }
 41     }
 42 }");
 43             //定义buffer缓冲区,把StringBuilder对象的内容转化成UTF8的字节类型放入缓冲区
 44             byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString());
 45             //从缓冲区中的内容向文件的第0个位置开始写,写入buffer.Length的字节长度
 46             fs.Write(buffer, 0, buffer.Length);
 47             //最后关闭FileStream流
 48             fs.Close();
 49         }
 50     }
 51 }
 52 //FileStream输出从文件中读取的内容
 53 using System;
 54 using System.Collections.Generic;
 55 using System.Linq;
 56 using System.Text;
 57 using System.IO;
 58 
 59 namespace ConsoleApplication1
 60 {
 61     class Program
 62     {
 63         static void Main(string[] args)
 64         {
 65             FileStream fs = File.Open(@"d:\1.cs", FileMode.Open);
 66             //默认缓冲区为1024字节,每次从文件流获取1024字节放入缓冲区
 67             byte[] buffer = new byte[1024];
 68             //i代表已经读取的字节数,若i=0,则已经读取文件末尾,从而结束循环
 69             int i = 0;
 70             string result = "";
 71             //当程序读取文件时,把从缓冲区读取的字节从第一个元素开始放置,直到buffer.Length
 72             while ((i = fs.Read(buffer, 0, buffer.Length)) != 0)
 73             {
 74                 result += Encoding.UTF8.GetString(buffer, 0, i);
 75             }
 76             //关闭流
 77             fs.Close();
 78             Console.WriteLine(result);
 79         }
 80     }
 81 }
 82 //FileStream从文件中只读取的1024字节内容(不完善的读操作)
 83 using System;
 84 using System.Collections.Generic;
 85 using System.Linq;
 86 using System.Text;
 87 using System.IO;
 88 
 89 namespace ConsoleApplication1
 90 {
 91     class Program
 92     {
 93         static void Main(string[] args)
 94         {
 95             FileStream fs = File.Open(@"d:\1.cs", FileMode.Open);
 96             byte[] buffer = new byte[1024];
 97             fs.Read(buffer, 0, buffer.Length);
 98             string result = Encoding.UTF8.GetString(buffer);
 99             fs.Close();
100             Console.WriteLine(result);
101         }
102     }
103 }
104 //FileStream 复制文件
105 using System;
106 using System.Collections.Generic;
107 using System.Linq;
108 using System.Text;
109 using System.IO;
110 
111 namespace ConsoleApplication1
112 {
113     class Program
114     {
115         static void Main(string[] args)
116         {
117             FileStream read = File.Open(@"d:\1.cs", FileMode.Open);
118             FileStream write = File.Open(@"e:\1.cs", FileMode.OpenOrCreate);
119             byte[] buffer = new byte[1024];
120             int i = 0;
121             while ((i = read.Read(buffer, 0, buffer.Length)) != 0)
122             {
123                 write.Write(buffer,0,i);
124             }
125             write.Close();
126             read.Close();
127         }
128     }
129 }
130 //FileStream 边复制文件边打印进度
131 namespace ConsoleApplication1
132 {
133     class Program
134     {
135         static void Main(string[] args)
136         {
137             FileStream read = File.Open(@"d:\1.cs", FileMode.Open);
138             //定义count,标识文件总大小
139             long count = read.Length;
140             FileStream write = File.Open(@"e:\1.cs", FileMode.OpenOrCreate);
141             byte[] buffer = new byte[1024];
142             int i = 0;
143             //定义ri,获取已经读取的文件
144             int ri = 0;
145             while ((i = read.Read(buffer, 0, buffer.Length)) != 0)
146             {
147                 write.Write(buffer,0,i);
148                 ri += i;
149                 string s = ((double)ri / count * 100).ToString("0.00") + "%";
150                 Console.WriteLine(s);
151             }
152             write.Close();
153             read.Close();
154         }
155     }
156 }
157 //FileStream 剪切文件:
158 using System;
159 using System.Collections.Generic;
160 using System.Linq;
161 using System.Text;
162 using System.IO;
163 
164 namespace ConsoleApplication1
165 {
166     class Program
167     {
168         static void Main(string[] args)
169         {
170         //复制操作
171             FileStream read = File.Open(@"d:\1.cs", FileMode.Open);
172             FileStream write = File.Open(@"e:\1.cs", FileMode.OpenOrCreate);
173             byte[] buffer = new byte[1024];
174             int i = 0;
175             while ((i = read.Read(buffer, 0, buffer.Length)) != 0)
176             {
177                 write.Write(buffer,0,i);
178             }
179             write.Close();
180             read.Close();
181         //复制后删除源文件
182         File.Delete(@"d:\1.cs");
183         }
184     }
185 }

二.StreamReader、StreamWriter对数据的有关操作

View Code
 1 //使用StreamReader单独操作字符进行读操作
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.IO;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //操作字符
15             //StreamReader reader = new StreamReader(@"d:\1.cs");
16         StreamReader reader = new StreamReader(@"d:\1.cs",Encoding.UTF8);
17             string str = reader.ReadToEnd();
18             Console.WriteLine(str);
19             reader.Close();
20         }
21     }
22 }
23 //用StreamReader通过ReadLine单行读取,操作字符输出读取的内容
24 using System;
25 using System.Collections.Generic;
26 using System.Linq;
27 using System.Text;
28 using System.IO;
29 
30 namespace ConsoleApplication1
31 {
32     class Program
33     {
34         static void Main(string[] args)
35         {
36             //操作字符
37             StreamReader reader = new StreamReader(@"d:\1.cs");
38             string str = reader.ReadLine();
39             while (str != null)
40             {
41                 Console.WriteLine(str);
42                 str = reader.ReadLine();
43             }
44             reader.Close();
45         }
46     }
47 }
48 //使用StreamWriter单独操作字符进行写操作    
49 using System;
50 using System.Collections.Generic;
51 using System.Linq;
52 using System.Text;
53 using System.IO;
54 
55 namespace ConsoleApplication1
56 {
57     class Program
58     {
59         static void Main(string[] args)
60         {
61             //操作字符,true表示执行写操作的时候在原内容基础上追加。
62             StreamWriter writer = new StreamWriter(@"d:\1.cs",true,Encoding.UTF8);
63             writer.Write("写入文件的内容:……");
64             writer.Close();
65         }
66     }
67 //使用FileStream流设置文件写操作时的追加效果设置(避免向文件中写内容时覆盖掉文件中原存内容),从而代替StreamWriter参数中true的设置,效果同上
68 using System;
69 using System.Collections.Generic;
70 using System.Linq;
71 using System.Text;
72 using System.IO;
73 
74 namespace ConsoleApplication1
75 {
76     class Program
77     {
78         static void Main(string[] args)
79         {
80             FileStream stream = File.Open(@"d:\1.cs", FileMode.Append);
81             StreamWriter writer = new StreamWriter(stream);
82             writer.Write("写入文件的内容:……");
83             writer.Close();
84         }
85     }
86 }

三.BinaryReader、BinaryWriter有关的数据操作

View Code
 1 //BinaryReader读操作
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.IO;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             FileStream stream = File.Open(@"d:\1.cs", FileMode.Open);

15             BinaryReader reader = new BinaryReader(stream);
16             byte[] buffer = new byte[1024];
17             int i = 0;
18             string str = "";
19             while ((i = reader.Read(buffer, 0, buffer.Length)) != 0)
20             {
21                 str += Encoding.UTF8.GetString(buffer, 0, i);
22             }
23             reader.Close();
24             stream.Close();
25             Console.WriteLine(str);
26         }
27     }
28 }
29 //BinaryWriter写操作
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Text;
34 using System.IO;
35 
36 namespace ConsoleApplication1
37 {
38     class Program
39     {
40         static void Main(string[] args)
41         {
42             FileStream stream = File.Open(@"d:\1.cs", FileMode.OpenOrCreate);
43             BinaryWriter writer = new BinaryWriter(stream);
44             string str = "这是写入的内容";
45             byte[] buffer = Encoding.UTF8.GetBytes(str);
46             writer.Write(buffer, 0, buffer.Length);
47             writer.Close();
48             stream.Close();
49         }
50     }
51 }

四.利用BufferedStream进行读写操作

View Code
 1 //BufferedStream读操作
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;

 6 using System.IO;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Stream streamReader = File.Open(@"d:\1.cs",FileMode.Open);
15             BufferedStream stream = new BufferedStream(streamReader);
16             byte[] buffer = new byte[1024];
17             int i = 0;
18             string str = "";
19             while ((i = streamReader.Read(buffer, 0, buffer.Length)) != 0)
20             {
21                 str += Encoding.UTF8.GetString(buffer, 0, i);
22             }
23             stream.Close();
24             streamReader.Close();
25             Console.WriteLine(str);
26         }
27     }
28 }
29 //BufferedStream写操作
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Text;
34 using System.IO;
35 
36 namespace ConsoleApplication1
37 {
38     class Program
39     {
40         static void Main(string[] args)
41         {
42             Stream streamWriter = File.Open(@"d:\1.cs",FileMode.OpenOrCreate);
43             BufferedStream stream = new BufferedStream(streamWriter);
44             StringBuilder sb = new StringBuilder();
45             sb.Append(@"这是写入的内容:……
46 ");
47             byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString());
48             stream.Write(buffer, 0, buffer.Length);
49             stream.Close();
50             streamWriter.Close();
51         }
52     }
53 }

了解了IO中流对数据的读写操作以及复制原理后,我们还可以使用File类的方法直接对文件执行复制、删除等操作:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //创建文件
14             //File.Create(@"d:\1.mp3");
15             //拷贝文件
16             //File.Copy(@"d:\1.mp3",@"d:\2.mp3");
17             //删除文件
18             //File.Delete(@"d:\2.mp3");
19             //移动文件
20             //File.Move(@"d:\1.mp3", @"e:\1.mp3");
21             //获取创建文件时间
22             DateTime dt = File.GetCreationTime(@"e:\1.mp3");
23             Console.WriteLine(dt);
24 
25         }
26     }
27 }

Directory     利用目录可以在制定位置直接创建文件夹等:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //创建文件夹
14             //Directory.CreateDirectory(@"d:\my");
15             //获取文件夹中所有文件
16             //string[] strs = Directory.GetFiles(@"d:\my");
17             //foreach (string str in strs)
18             //{
19             //    Console.WriteLine(str);
20             //}
21             //获取文件夹下的文件夹中的文件
22             string[] strs = Directory.GetFileSystemEntries(@"d:\my");
23             foreach (string str in strs)
24             {
25                 if (Directory.Exists(str))
26                 {
27                     string[] ss = Directory.GetFileSystemEntries(str);
28                     foreach (string s in ss)
29                     {
30                         Console.WriteLine(s);
31                     }
32                 }
33             }
34         }
35     }
36 }

 

 

转载于:https://www.cnblogs.com/yunqinglin/archive/2012/08/05/2623551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值