流的操作(复制文件) 读写文件

读流,就会读流指定的文件。
一个字节一个字节的读

 1 using System;
 2 using System.IO;
 3 
 4 namespace StreamWork
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             //这样的文件流是内存与硬盘之间的通道。包含硬盘上非托管的资源,存在硬盘上而不是存在内存上,要using和数据库connection command类似。
11             //File.Mode.open的时候,这个路径文件必须存在。否则报错
12             using (FileStream file = new FileStream("wenjian.txt", FileMode.Open, FileAccess.ReadWrite))
13             {
14                 //向这个流读写,因为这个流已经确定对应的文件。就会自动读写已经确定对应的文件
15                 //一个字节一个字节的读,返回此字节的unicode编码为 int类型,循环读取,便读玩。
16                 //当读的字节没有数据的时候,返回-1
17                 int res = -1;
18                 //do while前期定义变量初值, 先循环,后判断此变量条件。 while不行。就是对定义的变量
19                 //while (res!=-1)
20                 //{
21                 //}
22                 do
23                 {
24                     //会自动最后最后一个字节的后一位空,返回-1,要排除,否则就会多一项
25                     res = file.ReadByte();
26                     if (res != -1)
27                     {
28                         Console.WriteLine((char)res);
29                     }
30                 } while (res != -1);
31                 Console.ReadKey();
32             }
33         }
34     }
35 }

读一个字节数组file.Read/write/(字节数组,起始位,长度)
字符集.getstring/char(字节
数组,起始位,长度
getbytes(char字符数组)

要求按照每次读几个字节,读完整个文件

 using (FileStream file = new FileStream("wenjian.txt", FileMode.Open, FileAccess.ReadWrite))
           {//建立一个供存放几个字节的数组
               byte[] by=new byte[3];
               //将文件以按照3个字节的长度读,放到这个数组中区。
               //但是3个字节长度去读不一定能读3个字节,可能是2个1 个或者0个。这个就是read返回值,可变
               int readlen = 0;
               //外循环(只要读到的数据个数大于0,有数据就循环)例如15个字节 读有3个>0,成立继续读,知道第16个没有就停止
               while (( readlen = file.Read(by, 0, by.Length))>0)
               {//对这三个或者1  2的字节操作
                   string san = Encoding.Default.GetString(by, 0, readlen);
                       Console.WriteLine(san);
               }
           }
            Console.ReadKey();
            //Encoding.UTF8.GetString()
            //Encoding.Default                                     获取字符集。。。。。这些字符集具有getstring ()等等
             //Encoding.GetEncoding("gb2312")

向流写,就是想流指定的文件写。

1写一个字节,多个字节   int res=byte(字符) char res= char(unicode编码)

1 using (FileStream file = new FileStream("aaa.txt", FileMode.Create, FileAccess.ReadWrite))
2             {
3                 file.WriteByte(97);   //会在bin--debug创建建立aaa.txt,写入unicode码97对应的a值
4             }

2写入一个字符串。对同一个文件多次写入会清除后再写

1 using (FileStream file = new FileStream("aaa.txt", FileMode.Create, FileAccess.ReadWrite))
2            {
3                string str = "dasdasdasdjadasda";
4                for (int i = 0; i < str.Length; i++)
5                {
6                    file.WriteByte((byte)(str[i]));   
7                }
8             }

3写入汉字:Encoding.**=处理编码的类。写入一个数组。file.write(数组,起始位,长度)

 using (FileStream file = new FileStream("aaa.txt", FileMode.Create, FileAccess.ReadWrite))
           {
               byte[]  by = Encoding.UTF8.GetBytes("我是低洼大");
               //从0位开始,长度为字节数组长度
                   file.Write(by,0,by.Count());
            }

复制文件:不论视频什么文件都可以。

1,打开要复制的文件,create一个空间

2,建立一个数组,临时缓存,完成读写功能。每次读多少

3,while循环读写

 1 using (FileStream file = new FileStream(@"多态、委托、索引器.wmv",FileMode.Open,FileAccess.Read))
 2             {
 3                 using (FileStream cun=new FileStream(@"2.wmv",FileMode.Create,FileAccess.Write))
 4                 {//每次读取3M
 5                     byte[] by=new byte[1024*1024*3];
 6                     //int shengyu = file.Read(by, 0, by.Length);//这样就死循环了。比徐把这条语句放进while内,会一次一次判断
 7                     //while (shengyu>0)
 8                     //{
 9                     //    cun.Write(by,0,shengyu);
10                     //}
11                     int shengyu = 0;
12                     while ((shengyu = file.Read(by, 0, by.Length)) > 0)
13                     {
14                         cun.Write(by, 0, shengyu);
15                     }
16                 }
17             }

结果

补充:需要file.dispose和cun.dispose。只需要调用dispose就ok,这些子类流调用dispose就会调用父类的dispose和close方法。

显示复制进度数int fzshu=0;

在while内部 fzshu+=shengyu;cw(“{0}%“,sum*100/file)

读写流:streamReader/streamWriter

  using (FileStream file = new FileStream(@"wenjian.txt", FileMode.Open, FileAccess.Read))
            {
                StreamReader reader = new StreamReader(file, Encoding.Default);
                Console.WriteLine(reader.ReadToEnd());
                Console.ReadKey();
            }

文件流本身file就可以读写自己。还有streamreader流读文件流  ,

流读流

streamreader也有read(字节数组,起始,长度)也可以实现文件的复制。

原文地址:https://www.cnblogs.com/leee/p/4241886.html