• C#_File_Assembly


    //--------------------------------------------^ ^-----------------------------------------------------------

    static  void  Main(string[] args)

    {

      //1.get   current  assembly  location

      string    currentPath=Assembly.GetExecutingAssembly().Location;    //Get Executing Assembly()  

      string    currentDirectoryPath=Path.GetDirectoryName(currentPath);

      

      //2.create  a  file   under    current  directory  path

      FileStream   fs=File.Create(currentDirectoryPath+@" est.txt");

      fs.Close();

      

      string   msg="This is a test string..";

      //3.change  string to byte[]

      byte[]   bs=System.Text.Encoding.UTF-8.GetBytes(msg);

      File.WriteAllBytes(currentDirectoryPath+@" est.txt",bs);

      

      //4.change byte[]  to string 

      byte[]   rbs=File.ReadAllBytes(currentDirectoryPath+@" est.txt");

      string   rString=System.Text.Encoding.UTF-8.GetString(rbs);

      Console.WriteLine(rString);

    }

    //-----------------------------------------------------------------------------------------

    编码乱码问题:

     //英文占1个字节8个bit    中文占2个字节 16个bit        Unicode 国际码表 ->在该码表中 任何字符都占2个字节
    
                //文件头 标记文件的编码格式
    
                //UTF-8   国际码表:  英文占1个字节    中文占3个字节
    
    //乱码问题出现:  文件保存编码格式与读取文件的编码格式不一致
    View Code

     //----------------------------------------------------------------------------------------

    文件读写步骤:

        string txt="This is a filestream  test";

        byte[]  buffer= System.Text.Encoding.UTF8.GetBytes(txt);

        //1.创建文件流   

          FileStream fs=new FileStream(@"D: est.txt",FileMode.Create,FileAccess.Write);    //(文件位置,创建文件模式,权限)

        //2.写入文件

          fs.Write(buffer,0,buffer.Length);    //(要写入的数组,开始写入的位置一般是0,要写入的长度)

        //3.关闭文件流 清空缓冲区

          fs.Flush();

          fs.Close();

        //4.释放相关资源

          fs.Dispose();    //一般调用此步骤就行    包含第三步

    //---------------

      以上 不需要手动调用第四步的方法:    加using:

      using(FileStream fs=new FileStream(@"D:fileTest.txt",FileMode.Create,FileAccess.Write))    //当fs作用域超出此范围时候自动调用   fs.Dispose();

      {

        fs.Write(buffer,0,buffer.Length);

      }  

    //-----------------------------------------------------------------------------------------

      //调用文件流拷贝文件   

      string source=@"D:1.mov";

      string target=@"E:2.mov";

      CopyFile(source,target);

      

      private CopyFile(string source,source target)

      {

        //创建读文件流从源文件中读取放置在缓冲区中,创建写文件流从缓冲区中写入文件

        using(FileStream fsRead = new FileStream(source,FileMode.Open,FileAccess.Read))

        {

          using(FileStream fsWrite=new FileStream(target,FileMode.Creat,FileAccess.Write))

          {

            //创建缓冲区 用来存放每次读取的字节

            byte[] buffer =new byte[1024*1024*2];      //2M 缓冲区2M

            //参数1:读取之后存放的位置  缓冲区

            //参数2:开始存放字节的位置

            //参数3:可以读取的最多的字节数

              int byteReadCount=fsRead.Read(buffer,0,buffer.Length);

            //循环 :当读取到缓冲区中的字节大小不为零时 表明读取到了字节数

            while(byteReadCount>0)

            {

                //将读取到的文件写入

              fsWrite.Write(buffer,0,byteReadCount);

              //fsWrite.Position   记录当前已经写入的字节的长度

              byteReadCount=fsRead.Read(buffer,0,buffer.Length);

            }        

          }

        }      

      }

    //----------------------------------------------------------------------------------------------

    //使用StreamReader 和 StreamWriter 来对大型文本进行读写操作

     1     #region 工资读写Exercise
     2             using(StreamReader sReader=new StreamReader("工资文件.txt",Encoding.Default))
     3             {
     4                 using (StreamWriter sWriter = new StreamWriter("new.txt", false))
     5                 {
     6                     while (!sReader.EndOfStream)
     7                     {
     8                             string line = sReader.ReadLine();
     9                             string[] lineparts = line.Split(new char[] { '|'});
    10                             int doubleSalary = Convert.ToInt32(lineparts[1])*2;
    11                             lineparts[1] = doubleSalary.ToString();
    12                             string newline = lineparts[0] + "|" + lineparts[1];
    13                             sWriter.WriteLine(newline);
    14                     }
    15                 }
    16             }
    17             #endregion
    ReaderAndWriter
  • 相关阅读:
    1-直播转点播
    3-美团 HTTP 服务治理实践
    3-SSDB 高性能NoSQL数据库, 用于替代 Redis.
    配置kubectl在Mac(本地)远程连接Kubernetes集群
    4-rocketmq 发送时异常:system busy 和 broker busy 解决方案
    3-RocketMQ 简单梳理 及 集群部署笔记
    2-Rocketmq产品架构(参考阿里云)
    1-RocketMq 学习 中文文档(一)
    tar命令参数详解
    Ubuntu 安装 .bundle 文件
  • 原文地址:https://www.cnblogs.com/siyi/p/4831178.html
Copyright © 2020-2023  润新知