• c#FileStream



    1. //使用文件流读取数据
                FileStream fsRead = new FileStream(@"C:UsersAdministratorDesktopc#笔记泛型集合.txt" , FileMode.OpenOrCreate,FileAccess.Read );
                //定义字节数组
                Byte [] byt = new Byte[1024*1024];
                //将读取的数据放入字节数组中,并返回实际数据的大小
               int realData = fsRead.Read(byt , 0 , byt.Length);
                //将字节数组中,实际长度内容转换成字符串
               string stt = Encoding.Default.GetString(byt , 0 ,realData);
                //关闭文件流
               fsRead.Close();
                //释放流所占用的资源
               fsRead.Dispose();
               Console.WriteLine(stt);

                //使用FileStream 来写数据
                //使用using将不用自己写close  &&dispose 来关闭数据流,释放资源空间。
                using(FileStream wtrite = new FileStream(@"C:UsersAdministratorDesktopc#笔记泛型集合.txt",FileMode.Append , FileAccess.Write))
               {
                   string fswrite = "看我有没有把你改掉";
                   Byte[] newfs = Encoding.Default.GetBytes(fswrite);
                   wtrite.Write(newfs , 0 ,newfs.Length);
               }

    2.对多媒体文件复制到另一个路径

     static void Main(string[] args)
            {
                string sorceFile = @"F:学习资料c#c#亲情奉献-全套精品.Net基础班视频教程-video亲情奉献全套精品.Net基础视频教程之10-面向对象多态(第十二天)video14、最后小结.avi";
                string targetFile = @" F:学习资料c#c#亲情奉献-全套精品.Net基础班视频教程-video亲情奉献全套精品.Net基础视频教程之10-面向对象多态(第十二天)video ew.avi";

                FileCopy(sorceFile, targetFile);
                Console.ReadKey();
            }
            /// <summary>
            /// 将一个多媒体文件复制到另一个文件路径下。
            /// </summary>
            /// <param name="scour">源文件</param>
            /// <param name="target">目标文件</param>
            static void FileCopy(string scour, string target)
            {
                using (FileStream fsread = new FileStream(scour , FileMode.Open , FileAccess.Read))
                {
                    using (FileStream fsWrite = new FileStream( target, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        Byte []source = new Byte [1024*1024];
                        while(true)
                        {
                            int ds = fsread.Read(source, 0, source.Length);
                            if(ds == 0)
                            {
                                break;
                            }
                            //向目标文件中写数据
                            fsWrite.Write(source, 0, ds);
                        }
                    }

    3.StreamReader  StreamWriter
    using (StreamReader sr = new StreamReader(@"C:UsersAdministratorDesktopc#笔记 hird day.txt" , Encoding.Default ))
                {
                    //当没有读到文档结束符
                   while(!sr.EndOfStream)
                   {
                       Console.WriteLine(sr.ReadLine());
                   }
                }


                //使用StreamWrite向文本文档写数据

                using (StreamWriter sw = new StreamWriter(@"C:UsersAdministratorDesktopc#笔记 hird day.txt"))
                {
                    string swt = "今天天气很晴朗,我真的很想找个女朋友";
                    sw.Write(swt);

                }

  • 相关阅读:
    Linux下配置Tomcat服务器
    Octopress + GitHub Page 搭建个人博客
    Cocoapods报错Unable to satisfy the following requirements
    类方法load和initialize的区别
    AFNetworking3.0 Post JSON数据
    iOS防止button重复点击
    iOS与H5交互遇到的坑
    cocoapods安装
    平时做的一些好玩或者测试的 项目 特效,动画
    webpack那些事儿
  • 原文地址:https://www.cnblogs.com/jefy/p/9207384.html
Copyright © 2020-2023  润新知