zoukankan      html  css  js  c++  java
  • FileStream使用小记

    流用于对IO处理 

    在System.IO名称空间中有以下类 

    BinaryReader/Writer 

    TextReader/Writer 

    Stream 

    其中类Stream为抽象类。由此有三个派生类: 

    MemoryStream:对内存进行读取与写入 

    BufferedStream:对缓冲器进行读取/写入 

    FileStream:对文件执行读取与写入 

    TextReader/Writer为抽象类。由此派生类: 

    StreamReader/StreamWirter 

    StringReader/StringWriter 

    文件流Write使用:

    String path=@"D:aa.Text";
    using(FileStream fs=new FileStream(path,FileModel.Open,FileAccess.ReadWrite)){
      byte[] body=new byte[fs.Length];
      fs.Read(body,0,(int)body.Length);
      byte[] head=new byte[44];
      fs.Write(head,0,(int)head.Length);
      fs.Write(body,0,(int)body.Length);
    }

    这块在拼接write时,第三个参数的意思<要写入当前流的长度>,就是说当前数组的长度而不是写到流的长度。

    文件流Read使用:

    1 String path=@"D:aa.Text";
    2 byte[] all=null;
    3 using(FileStream fs=new FileStream(path,FileModel.Open,FileAccess.Read)){
    4    all=new byte[44+path.Length];
    5     //先在Byte[]中写入44个字节
    6    fs.Read(all,44,(int)fs.Length);
    7 }

    这块在Read时,第三个参数的意思<最多读取的字节数>,不是数组的长度是要读取多少字节

    作者:大胖儿在努力 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    加入创业公司有什么利弊
    Find Minimum in Rotated Sorted Array II
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Find Minimum in Rotated Sorted Array
    Remove Duplicates from Sorted Array
    Spiral Matrix
    Spiral Matrix II
    Symmetric Tree
    Rotate Image
  • 原文地址:https://www.cnblogs.com/cuijl/p/4553734.html
Copyright © 2011-2022 走看看