• C#常用的流类型(FileStream,SteamWriter/StreamReader,MemoryStream等)


    一.FileStream类

    用途:主要用于读写磁盘文件。常用于向磁盘存储数据或读取配置文件。

    优点:该类维护内部文件指针,当处理大型文件时非常省时,可以定位到正确的位置。

    缺点:类是通过字节形式来读写数据的,需要自己处理编码转换,把字节数据转换为文本。

    读取数据:

    1            string path = @"D:hello.txt";
    2             FileStream fs = new FileStream(path, FileMode.Open);    //初始化文件流
    3             byte[] arr = new byte[fs.Length];                      //初始化字节数组
    4             fs.Read(arr, 0, arr.Length);                           //从流中数据读取到字节数组中
    5             fs.Close();//关闭流
    6             string str = Encoding.UTF8.GetString(arr);             //将字节数组转换为字符串
    7             Console.WriteLine(str);
    View Code

    写入数据:

     1            string path = "";
     2             string targetPath = "";
     3             FileStream source = new FileStream(path, FileMode.Open,FileAccess.Read);
     4             FileStream target = new FileStream(targetPath,FileMode.Create, FileAccess.Write);
     5             byte[] buff = new byte[10240];  //1KB
     6             int bytesRead = 0;
     7             do
     8             {
     9                 bytesRead = source.Read(buff, 0, buff.Length);
    10                 target.Write(buff, 0, bytesRead);
    11             } while (bytesRead>0);
    12             source.Dispose();
    13             target.Dispose();
    View Code

    注意:当打开文件或者创建文件时,流指针默认位于文件头,当调用Read/Write后,流文件会自动向后移动相应的字节数。因此在代码中无须设置,每次调用Read和Write时,读取或写入都是尚未处理过的字节。

    二.SteamWriter类 / StreamReader类

    特点:用于对文本的读取和写入。

    读取数据:

    1             string path = "";
    2             FileStream fs = new FileStream(path, FileMode.Open);  //初始化文件流
    3             StreamReader sr = new StreamReader(fs);              //初始化StreamReader
    4             string one_line = sr.ReadLine();                     //直接读取一行
    5             string total_line = sr.ReadToEnd();                  //读取全文
    6             sr.Close();                                          
    7             fs.Close();  
    View Code

    写入数据:

    1             string path = "";
    2             FileStream fs = new FileStream(path, FileMode.Append); //初始化文件流
    3             StreamWriter sw = new StreamWriter(fs);               //初始化StreamWriter
    4             sw.WriteLine("hello");                                //写入一行数据
    5             sw.Close();                                           //关闭流
    6             fs.Close();                                           //关闭流
    View Code

    三.MemoryStream类

    用途:内存流类,主要用于操作内存中的数据。比如说网络中传输数据时可以用流的形式,当我们收到这些流数据时就可以声明 MemoryStream 类来存储并且处理它们。

    1             string str = "Hello,World!";
    2             byte[] arr = Encoding.UTF8.GetBytes(str);        //将字符串转换为字节数组
    3             MemoryStream ms = new MemoryStream(arr);           //初始化MemoryStream类
    4             byte[] arrNew = ms.ToArray();                    //将内存中的数据转换为字节数组
    5             string strNew = Encoding.UTF8.GetString(arrNew); //将字节数组转换为字符串
    View Code

     

  • 相关阅读:
    Kibana: missing authentication credentials for REST request
    MySQL命令速记
    VIM常用命令简记
    Linux常用命令简记
    袁永福的C#编程书籍,电子工业出版社出版。
    发布DCWriter电子病历文本编辑器
    袁永福的博客系列文章链接集合
    ssh隧道 的命令行和 sshd 服务的配置(gatePort)
    PureMVC(AS3)剖析:设计模式(二)
    Python应用与实践
  • 原文地址:https://www.cnblogs.com/mariolz/p/13231706.html
Copyright © 2020-2023  润新知