.net中的IO体系介绍

  .net中对于IO流的支持分为两个层次:基于字节和基于字符两种方式。
基于字节的方式:
基于字节的方式适用于任何场合,因为任何文件的数据都是基于字节的方式有序存放的。基于字节的方式适用于操作二进制文件,比如exe文件、视频、音频文件等等。
Stream抽象类是所有基于字节方式的流的父类。Stream及其子类具有三个方面的特征:
支持从流读取。可以从流中读取单个字节或者一次性读取多个字节,将读取到的多个字节存储到字节数组中。
支持向流写入。可以向流中写入单个字节或者一次性写入多个字节,如果要一次性写入多个字节,可以将多个字节构成一个字节数组,作为写入方法的参数。

支持查找。不过这个特性在其某些子类中就不支持,比如网络流。

 

Stream类及其部分子类UML图
Stream的常用方法定义:
public abstract void Write(byte[] buffer, int offset, int count);
上面的方法作用是将buffer这个字节数组中从offset字节开始的count个字节写入到流中,并且将流的位置向前推进写入的字节个数个字节。
public abstract void WriteByte(byte value);
上面的方法是一次向流中写入一个字节,并且将流的位置向前推进一个字节。
public abstract void Read(byte[] buffer, int offset, int count);
上面的方法作用是从流中第offset字节开始的count个字节读入到buffer这个字节数组中,并且将流的位置向前推进读取的字节个数个字节。
public abstract int ReadByte(); 上面的方法是从流中读取一个字节,并且将流的位置向前推进一个字节,如果已经达到流的结尾,则返回 -1

继承Stream抽象类的子类有: System.Data.OracleClient.OracleBFile:表示托管 OracleBFile 对象,该对象的设计旨在与 Oracle BFILE 数据类型配合使用。  System.Data.OracleClient.OracleLob:表示存储在 Oracle 服务器上的大型对象二进制 (LOB) 数据类型。 System.IO.BufferedStream:给另一流上的读写操作添加一个缓冲层。 System.IO.Compression.DeflateStream:提供用于使用 Deflate 算法压缩和解压缩流的方法和属性。 System.IO.Compression.GZipStream:提供用于压缩和解压缩流的方法和属性。 System.IO.FileStream:公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。 System.IO.MemoryStream:创建其支持存储区为内存的流。 System.Net.Sockets.NetworkStream:提供用于网络访问的基础数据流。 System.Security.Cryptography.CryptoStream:定义将数据流链接到加密转换的流。

基于字符的方式
对于东亚语系的国家而言,每个字符都是占用两个字节(采用Unicode编码),每次向文件读取和写入的时候基于字节的方式相对较为繁琐:写入的时候需要将字符串转换成字节数组,读取的时候需要将读取到的字节数组转换成字符串。为此,.net提供了一种更直接的方式,那就是基于字符的操作。
基于字符的方式相对就方面多了,我们可以指定一次性读/写入多个字符,或者一次性读/写入一行,或者一次性写入一个字符串,或者一次性将流中的所有字符读取。
在.net中基于字符的方式对流进行读写分别是由两个抽象类及其子类来完成的。
基于字符的方式读取
TextReader的常见方法定义:
public virtual int Peek();
这个方法的作用是:读取下一个字符,而不更改读取器状态或字符源。返回下一个可用字符,而实际上并不从输入流中读取此字符。
public virtual int Read();
这个方法的作用是:读取输入流中的下一个字符并使该字符的位置提升一个字符。这个方法的返回值,如果到流的末尾,则返回-1。
public virtual int Read (char[] buffer, int index, int count);
这个方法的作用是:从当前流中读取最大 count 的字符并从 index 开始将该数据写入 buffer。这个方法的返回值小于或等于 count,具体取决于流中是否有可用的数据。如果调用此方法时没有更多的字符留下可供读取,则此方法返回 0。
public virtual void ReadBlock(char[] buffer, int index, int count);
这个方法的作用是:从当前流中读取最大 count 的字符并从 index 开始将该数据写入 buffer。这个方法的返回值表示当前实际读取的字符数,如果已经达到文件的末尾,则返回值将小于count。
public virtual string ReadLine();
这个方面的作用是:从当前流中读取一行字符并将数据作为字符串返回。
public virtual string ReadToEnd();
这个方法的作用是:读取从当前位置到 TextReader 的结尾的所有字符并将它们作为一个字符串返回。
基于字符的方式写入
 
TextWriter抽象类常见方法及解释:
public virtua lvoid Flush();
这个方法的作用是:清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。
public virtual void Write(char value);
这个方法的作用是:将字符写入文本流。
public virtual void Write(char[] buffer);
这个方法的作用是:将字符数组写入文本流。
public virtual void Write(char[] buffer,int index,int count);
这个方法的作用是:将字符的子数组写入文本流。
public virtual void Write(string format, object[] arg0);
这个方法的作用是:使用与 String.Format 相同的语义写出格式化的字符串。
用法示例:
  1. using System;   
  2.   
  3. using System.Collections.Generic;   
  4.   
  5. using System.Text;   
  6.   
  7. using System.IO;   
  8.   
  9.   
  10.   
  11. namespace IODemo   
  12.   
  13. {   
  14.   
  15.     /// <summary>   
  16.   
  17.     /// 说明:基于字节的方式读写流的例子。   
  18.   
  19.     /// 作者:周公   
  20.   
  21.     /// 日期:2008-6-30   
  22.   
  23.     /// 原文地址:http://blog.csdn.net/zhoufoxcn   
  24.   
  25.     /// </summary>   
  26.   
  27.     public class StreamDemo   
  28.   
  29.     {   
  30.   
  31.         private string fileName;   
  32.   
  33.         /// <summary>   
  34.   
  35.         /// 要进行读写的文件全路径   
  36.   
  37.         /// </summary>   
  38.   
  39.         public string FileName   
  40.   
  41.         {   
  42.   
  43.             get { return fileName; }   
  44.   
  45.             set { fileName = value; }   
  46.   
  47.         }   
  48.   
  49.         /// <summary>   
  50.   
  51.         /// 基于字节的方式写文件流   
  52.   
  53.         /// </summary>   
  54.   
  55.         /// <param name="message"></param>   
  56.   
  57.         public void WriteFile(string message)   
  58.   
  59.         {   
  60.   
  61.             if (fileName != null)   
  62.   
  63.             {   
  64.   
  65.                 FileInfo file = new FileInfo(fileName);   
  66.   
  67.                 FileStream stream = null;   
  68.   
  69.                 //将要写入的字符串转换成utf8编码的字节数组   
  70.   
  71.                 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);   
  72.   
  73.                 if (!file.Exists)   
  74.   
  75.                 {   
  76.   
  77.                     //如果不存在指定文件则创建指定文件   
  78.   
  79.                     stream = file.Create();   
  80.   
  81.                 }   
  82.   
  83.                 else  
  84.   
  85.                 {   
  86.   
  87.                     //否则打开打开文件流   
  88.   
  89.                     stream = file.OpenWrite();   
  90.   
  91.                 }   
  92.   
  93.                 //将字符串转换的字节数组写入到流中   
  94.   
  95.                 stream.Write(buffer, 0, buffer.Length);   
  96.   
  97.                 stream.Close();//关闭流   
  98.   
  99.             }   
  100.   
  101.             else  
  102.   
  103.             {   
  104.   
  105.                 throw new ArgumentNullException("没有指定文件名异常");   
  106.   
  107.             }   
  108.   
  109.         }   
  110.   
  111.         /// <summary>   
  112.   
  113.         /// 基于字节的方式读文件流   
  114.   
  115.         /// </summary>   
  116.   
  117.         /// <returns></returns>   
  118.   
  119.         public string ReadMessage()   
  120.   
  121.         {   
  122.   
  123.             string result = string.Empty;   
  124.   
  125.             if (fileName != null)   
  126.   
  127.             {   
  128.   
  129.                 FileInfo file = new FileInfo(fileName);   
  130.   
  131.                 FileStream stream = file.OpenRead();   
  132.   
  133.                 byte[] buffer = new byte[(int)file.Length];   
  134.   
  135.                 stream.Read(buffer, 0, (int)(file.Length));   
  136.   
  137.                 //将读取到的字符串按照utf8方式转换成字符串   
  138.   
  139.                 result = System.Text.Encoding.UTF8.GetString(buffer);   
  140.   
  141.                 stream.Close();//关闭流   
  142.   
  143.             }   
  144.   
  145.             else  
  146.   
  147.             {   
  148.   
  149.                 throw new ArgumentNullException("没有指定文件名异常");   
  150.   
  151.             }   
  152.   
  153.             return result;   
  154.   
  155.         }   
  156.   
  157.        
  158.   
  159.     }   
  160.   
  161. }  
  1. 基于字符的方式:  
  1. <PRE class=csharp name="code">using System;   
  2.   
  3. using System.Collections.Generic;   
  4.   
  5. using System.Text;   
  6.   
  7. using System.IO;   
  8.   
  9.   
  10.   
  11. namespace IODemo   
  12.   
  13. {   
  14.   
  15.     /// <summary>   
  16.   
  17.     /// 说明:基于字符的方式读写流的例子。   
  18.   
  19.     /// 作者:周公   
  20.   
  21.     /// 日期:2008-6-30   
  22.   
  23.     /// 原文地址:http://blog.csdn.net/zhoufoxcn   
  24.   
  25.     /// </summary>   
  26.   
  27.     public class TextDemo   
  28.   
  29.     {   
  30.   
  31.         private string fileName;   
  32.   
  33.         /// <summary>   
  34.   
  35.         /// 要进行读写的文件全路径   
  36.   
  37.         /// </summary>   
  38.   
  39.         public string FileName   
  40.   
  41.         {   
  42.   
  43.             get { return fileName; }   
  44.   
  45.             set { fileName = value; }   
  46.   
  47.         }   
  48.   
  49.         /// <summary>   
  50.   
  51.         /// 基于字符的方式写文件流   
  52.   
  53.         /// </summary>   
  54.   
  55.         /// <param name="message"></param>   
  56.   
  57.         public void WriteFile(string message)   
  58.   
  59.         {   
  60.   
  61.             if (fileName != null)   
  62.   
  63.             {   
  64.   
  65.                 FileInfo file = new FileInfo(fileName);   
  66.   
  67.                 FileStream stream = null;   
  68.   
  69.                 StreamWriter writer = null;   
  70.   
  71.                 if (!file.Exists)   
  72.   
  73.                 {   
  74.   
  75.                     //如果不存在指定文件则创建指定文件   
  76.   
  77.                     stream = file.Create();   
  78.   
  79.                 }   
  80.   
  81.                 else  
  82.   
  83.                 {   
  84.   
  85.                     //否则打开打开文件流   
  86.   
  87.                     stream = file.OpenWrite();   
  88.   
  89.                 }   
  90.   
  91.                 writer = new StreamWriter(stream);   
  92.   
  93.                 //将字符串写入流中   
  94.   
  95.                 writer.Write(message);   
  96.   
  97.                 writer.Close();   
  98.   
  99.                 stream.Close();//关闭流   
  100.   
  101.                    
  102.   
  103.             }   
  104.   
  105.             else  
  106.   
  107.             {   
  108.   
  109.                 throw new ArgumentNullException("没有指定文件名异常");   
  110.   
  111.             }   
  112.   
  113.         }   
  114.   
  115.         /// <summary>   
  116.   
  117.         /// 基于字符的方式读文件流   
  118.   
  119.         /// </summary>   
  120.   
  121.         /// <returns></returns>   
  122.   
  123.         public string ReadMessage()   
  124.   
  125.         {   
  126.   
  127.             string result = string.Empty;   
  128.   
  129.             if (fileName != null)   
  130.   
  131.             {   
  132.   
  133.                 FileInfo file = new FileInfo(fileName);   
  134.   
  135.                 FileStream stream = file.OpenRead();   
  136.   
  137.                 StreamReader reader = new StreamReader(stream);   
  138.   
  139.                 //一次性读入所有字符   
  140.   
  141.                 result = reader.ReadToEnd();   
  142.   
  143.                 reader.Close();   
  144.   
  145.                 stream.Close();//关闭流   
  146.   
  147.                    
  148.   
  149.             }   
  150.   
  151.             else  
  152.   
  153.             {   
  154.   
  155.                 throw new ArgumentNullException("没有指定文件名异常");   
  156.   
  157.             }   
  158.   
  159.             return result;   
  160.   
  161.         }   
  162.   
  163.     }   
  164.   
  165. }</PRE>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值