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 _03FireStream文件夹流
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 // FileStream 读取和写入一个文件,一点点读取,可以按照指定读取字节大小读取,
15 //FileStream(文件夹名字,创建的模式,读取的形式)他是一个非静态类,所以用new创建
16 //先实现读取
17 FileStream fileRead = new FileStream("读文件.txt", FileMode.OpenOrCreate, FileAccess.Read);
18 //制定开辟字节的大小
19 byte[] buff = new byte[1024 * 1024 * 5];//是5m
20 int num = fileRead.Read(buff, 0, buff.Length); ;//理解成读取到了有效字节数为num 开辟的空间和要存储的空间不一致,所以要实际的
21 //因为是字节,要的是字符串,所以要把字节转换为字符串
22 string st = Encoding.Default.GetString(buff, 0, num);
23 fileRead.Close();//关闭流
24 fileRead.Dispose();//释放流(因为GC不能回收)
25 Console.WriteLine(st);//读取文件里 内容
26
27 // //*****************方法一 * **********************
28 //// 实现写 的功能
29 // FileStream fileWrite = new FileStream("写文件.txt", FileMode.OpenOrCreate, FileAccess.Write);
30 // //写入的是字符串
31 // string s = "465789";
32 // //存在文本文件里的东西是字节,所以要把字符串转换成字节
33 // byte[] bu = Encoding.Default.GetBytes(s);
34 // fileWrite.Write(bu, 0, bu.Length);
35 // fileWrite.Close();// 关闭流
36 // fileWrite.Dispose();// 释放流(因为GC不能回收)
37
38 //*****************方法二***********************
39 //用using可以省略写 fileWrite.Close();// 关闭流。fileWrite.Dispose();// 释放流(因为GC不能回收)
40 using (FileStream fileWrite = new FileStream("写文件.txt", FileMode.OpenOrCreate, FileAccess.Write))
41 {
42 string s = "465789";
43 //存在文本文件里的东西是字节,所以要把字符串转换成字节
44 byte[] bu = Encoding.Default.GetBytes(s);
45 fileWrite.Write(bu, 0, bu.Length);
46 fileWrite.Close();// 关闭流
47 fileWrite.Dispose();// 释放流(因为GC不能回收)
48 }
49 }
50 }
51 }