日斋
日新月异
using System;
using System.IO;

public class Block
{
    public static void Main()
    {
        Stream s = new MemoryStream();
        for (int i=0; i<100; i++)
            s.WriteByte((byte)i);
        s.Position = 0;

        // Now read s into a byte buffer.
        byte[] bytes = new byte[s.Length];
        int numBytesToRead = (int) s.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            // Read may return anything from 0 to numBytesToRead.
            int n = s.Read(bytes, numBytesRead, numBytesToRead);
            // The end of the file is reached.
            if (n==0)
                break;
            numBytesRead += n;
            numBytesToRead -= n;
        }
        s.Close();
        // numBytesToRead should be 0 now, and numBytesRead should
        // equal 100.
        Console.WriteLine("number of bytes read: "+numBytesRead);
    }
}
posted on 2010-01-27 10:47  李承隆  阅读(150)  评论(0编辑  收藏  举报