文件 流写入、流读取。

利用File类的Open方法打开一个目录,返回结果是FileStream 类型,(文件流)

FileStream myfile = File.Open(@"d:\2012级联系方式.txt", FileMode.OpenOrCreate, FileAccess.Read)

//myfile本身有.Read.Write方法,不过需要二进制的参数,不过使用myfile本身的方法,可以读写任何文件(音乐、图片、记事本等)并非文件夹。
myfile .Read(byte[] array, int offset, int count);
myfile .Write(byte[] array, int offset, int count);

例子一:(读出文件内容)
class Program
    {
        static void Main(string[] args)
        {
            FileStream fsreader = File.Open(@"d:\2012级联系方式.txt", FileMode.OpenOrCreate, FileAccess.Read);
           

            //定义缓冲区,也就是字节数组,将记事本中文字首先保存到字节数组中,然后在将字节数组中的内容写入到一个新的文本文件中
            //byte就是字节类型,byte类型数组中的每一个元素都是byte类型,而byte类型(字节类型)只能存储一个字节的数据,所以如果想让byte数组中存储5个字节的数据,此数组的元素数量就应该为5.
            byte[] buffer=new byte[20];
            //从流中读取数据,并放到缓冲区中
            fsreader.Read(buffer, 0, buffer.Length);
            //将二进制数组转换为字符串
            string str= Encoding.UTF8.GetString(buffer);
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
例子二:(将文件中的内容,写入另一个文本文档中)

 class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fsreader = File.Open(@"d:\2012级联系方式.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                //Length属性的值就是流的长度,流的长度就是记事本的内容长度,而记事本的内容长度就是记事本中内容全部转换为字节之后的长度,也就是记事本中内容的总的自字节数量。
                byte[] buffer = new byte[fsreader.Length];
                //从流中读取数据,并放到缓冲区中
                fsreader.Read(buffer, 0, buffer.Length);
                //将字节写到一个新的记事本中
                using (FileStream fswriter = File.Open(@"d:\newcontent.txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fswriter.Write(buffer, 0, buffer.Length);
                    Console.WriteLine("写入成功");
                }
            }


            Console.ReadKey();
        }
    }

例子三:(实现复制):targetfile的路径要写全,即要将文件的命名写好,如果只写E盘(即复制到E盘)会报错。

 class Program
    {
        static void Main(string[] args)
        {
            
            string sourcefile = @"F:\soft\EIE11_ZH-CN_MSN_WIN7.EXE";
            string targetfile = @"E:\EIE11_ZH-CN_MSN_WIN7.EXE";

            //Copy方法:将文件全部读取到内存中,然后在放置到E盘下,会占用内存
            //File.Copy(sourcefile,targetfile);

            //如果希望精确控制每次读取和写入的大小,就要使用二进制的方式读写。
            CopyFile(sourcefile, targetfile);
            Console.WriteLine("复制成功");
            Console.ReadKey();
        }
        static void CopyFile(string sourcefile, string targetfile)
        {
            //创建了一个文件读取流
            using (FileStream fsreader=File.Open(sourcefile,FileMode.Open,FileAccess.Read))
            {
                //创建一个缓冲区,就是一个字节数组
                byte[] buffer=new byte[1024*1024*10];
                //创建了一个流,用来写入字节
                using (FileStream fswriter=File.Open(targetfile,FileMode.OpenOrCreate,FileAccess.Write))
                {
                    while (true)
                    {
                        int count= fsreader.Read(buffer, 0, buffer.Length);
                        if (count <= 0)
                        {
                            break;
                        }
                        else
                        {
                            fswriter.Write(buffer, 0, buffer.Length);
                            //获取当前复制的内容的总共的长度,然后除以内容的总长度,得出的就是百分比
                            double sss = (double)fsreader.Position / fsreader.Length;
                            double positon = Math.Floor(sss*100);
                            Console.WriteLine("当前复制{0}",positon+"%");
                        }
                    }                    
                }
            }
        }
    }

如果仅操作文本文档,那么可以使用StreamReader和StreamWriter进行读写

例子:

  #region 使用using释放资源
            using (FileStream fs = File.Open(@"d:\2012级联系方式.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (StreamReader reader = new StreamReader(fs))
                {
                    string line = "";
                    using (FileStream fsw = File.Open(@"d:\new.txt", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(fsw))
                        {
                            while (!string.IsNullOrEmpty((line = reader.ReadLine())))
                            {
                                writer.WriteLine(line);
                            }

                            Console.WriteLine("ok");
                        }
                    }
                }
            }
            #endregion


注意:

1、在读出数据时,必须要有一个变量来接,比如上面的line ,要不读出的数据会有错误。

2、对于乱码还是用Encoding类来解决,即

StreamReader reader = new StreamReader(fs, Encoding.Default)  //读文件时乱码处理

StreamWriter writer = new StreamWriter(fsw,Encoding.Default)  //写入内容时

3、读或写时一定要释放资源,要不读出的数据会少,写入数据时,写不去。

使用using释放资源,或每一个由Stream(流)的对象,都要Close()并且Dispose()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值