• 文件流和字节流的操作


    File 类 定义在System的命名空间中
    用于 创建、复制 删除 移动和打开文件
    Create(string filePath)创建文件
    Open 打开文件
    Copy 复制文件
    Move (string SfilePath,string dFilePath)将指定的路劲的文件移动
    目标的路劲
    Exists (string filePath)//判断文件是否存在
    FileInfo 是一个静态类

    Directory 类
    //特点:静态类,不能实例化,类中都是静态方法


    ——————————————————————————————------------------------------------
    FileStream 类 表示操作文件流
     /*字节流使用步骤:
    1、创建字节流
    2、进行读写操作
    3、关闭流
    1.创建字节流
    FileStream  fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
    2、进行写操作
     byte[] bytes = Encoding.Default.GetBytes(mes sage);//将字符串根据指定的编码转换为字节数组(注意:写操作编码要和读操作编码要保持一致,否则容易出现乱码)
     fs.Write(bytes, 0, bytes.Length);//参数一:存数据的字节数组  参数二:从数组的哪个下标开始写入数据  参数三:要写入的字节数
    /3、关闭流
     fs.Close();
    读的操作
     //1、创建字节流
      FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read);
     byte[] bytes = new byte[fs.Length];//创建字节数组,存储从文件中读取到的字节数据,fs.Length获取文件大小,如果文件太大不要使用这种方法(因为会占用很大的内存空间)
      //2、读操作
      int len = fs.Read(bytes, 0, bytes.Length);//len存储了读取数据的字节数
      Console.WriteLine(len);
      string result = Encoding.Default.GetString(bytes);//将字节数组转换为字符串
      Console.WriteLine(result);
      //3、关闭流
      fs.Close();

    string path = @"e:my.txt";
    FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
     byte[] bytes = new byte[3];
     int len = 0;//存储每次读取的实际字节数
     while ((len=fs.Read(bytes, 0, bytes.Length))> 0)
    {
          Console.WriteLine(len);
          string s = Encoding.Default.GetString(bytes,0,len);//参数二:开始转换的下标  参数三:要转换的字节数
          Console.WriteLine(s);
    }

    fs.Close();
    ----------------------------------------------------
       /*字符流使用步骤:
     * 1、创建字节流
      * 2、创建字符流(包装、封装字节流)
      * 3、进行读写操作
      * 4、关闭流
        string path = @"e:my.txt";
        //1、创建字节流
        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
        //2、创建字符流(包装、封装字节流)
        StreamWriter sw = new StreamWriter(fs);
        //3、进行写操作
         sw.WriteLine("你好!");
         sw.WriteLine("hello world!");
       //4、关闭流
         sw.Close();
            //    fs.Close();

     string path = @"e:my.txt";
     FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
     StreamReader sr = new StreamReader(fs);
       //读到结尾
     string result = sr.ReadToEnd();
    Console.WriteLine(result);
      //读一行数据
     //string s = sr.ReadLine();
     //Console.WriteLine(s);

    string str = string.Empty;
     while ((str=sr.ReadLine())!= null)
      {
          Console.WriteLine(str);
        }
      sr.Close();
     fs.Close();
    --------------------------

  • 相关阅读:
    Context对象还提供了相应的属性来调整线条及填充风格
    基本类型互相之间转化可以用Covent类来实现。
    chfn是用来改变你的finger讯息
    在Web根目录下建立testdb.php文件内容
    springmvc接口接收json类型参数设置
    表单类型参数样板
    git push 免密码
    git提交之后没有push,代码被覆盖之后恢复
    测试流程总结
    linux 更改时区
  • 原文地址:https://www.cnblogs.com/cl1006/p/4275078.html
Copyright © 2020-2023  润新知