Stream Reading

          The abstract Stream class is the base for all streams. It defines methods and properties for three fundamental operations : reading, writing, and seeking , as well as for administrative tasks such as closing , flushing, and configuring timeouts.
          Reading: 
ContractedBlock.gif ExpandedBlockStart.gif Code

ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 从当前流读取序列字节,并且将次流的位置提升 count 个字节数.
        
/// </summary>
        
/// <param name="buffer">字节数组</param>
        
/// <param name="offset">buffer字节数组的读取位置</param>
        
/// <param name="count">读取的字节数量</param>
        
/// <returns></returns>

        public abstract int Read(byte[] buffer, int offset,int count)

ExpandedBlockStart.gifContractedBlock.gif       
/**////<summary>
       
/// 从流中读取一个字节,并将流内的位置向前推进一个字节,或者如果已到达流的末尾,则返回 -1
       
///</summary>

       public virtual int ReadByte();


         例子:
ContractedBlock.gif ExpandedBlockStart.gif Code
           // 创建一个test.txt在当前路径下, test.txt 中的内容是 12345678
            using (var stream =new FileStream("test.txt",FileMode.OpenOrCreate))
ExpandedBlockStart.gifContractedBlock.gif            
{
                var block 
= new byte[stream.Length];

                Console.WriteLine(stream.ReadByte()); 
// 49           //每次读一次 Position 的值自动加1
                Console.WriteLine(stream.ReadByte()); // 50

                stream.Position 
= 0;   // 把流位置设置为起始位置,经过上面 两行代码, Position 的值是2

                Console.WriteLine(stream.Read(block,
0,block.Length)); //8
                Console.WriteLine(stream.Read(block, 0, block.Length)); //0      因为 Position 已经到了末尾
            }



转载于:https://www.cnblogs.com/coolhao-chen/archive/2009/09/02/1558940.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值