MSDN上说是基础流,能不能解释的通俗一点。MSDN上有个例子,他是先获得文件流streamreader,然后用sr.BaseStream.Length来获得文件的长度,这一不理解?

解决方案 »

  1.   

    using System;
    using System.IO;class BinaryRW
    {
        static void Main()
        {
            int i;
            const int arrayLength = 1000;        // Create random data to write to the stream.
            Random randomGenerator = new Random();
            double[] dataArray = new double[arrayLength];
            for(i = 0; i < arrayLength; i++)
            {
                dataArray[i] = 100.1 * randomGenerator.NextDouble();
            }        using(BinaryWriter binWriter = 
                new BinaryWriter(new MemoryStream()))
            {
                // Write the data to the stream.
                Console.WriteLine("Writing data to the stream.");
                for(i = 0; i < arrayLength; i++)
                {
                    binWriter.Write(dataArray[i]);
                }            // Create a reader using the stream from the writer.
                using(BinaryReader binReader = 
                    new BinaryReader(binWriter.BaseStream))
                {
                    try
                    {
                        // Return to the beginning of the stream.
                        binReader.BaseStream.Position = 0;                    // Read and verify the data.
                        Console.WriteLine("Verifying the written data.");
                        for(i = 0; i < arrayLength; i++)
                        {
                            if(binReader.ReadDouble() != dataArray[i])
                            {
                                Console.WriteLine("Error writing data.");
                                break;
                            }
                        }
                        Console.WriteLine("The data was written " +
                            "and verified.");
                    }
                    catch(EndOfStreamException e)
                    {
                        Console.WriteLine("Error writing data: {0}.",
                            e.GetType().Name);
                    }
                }
            }
        }
    }
    看看这段代码msdn上的
      

  2.   

    FileStream fs = new FileStream ( "", FileMode.Open , FileAccess.Read ) ;
    StreamReader sr= new StreamReader ( fs ) ; 
     //使用StreamReader类来读取文件
    sr.BaseStream.Seek (0 , SeekOrigin.Begin ) ;
      

  3.   

    Stream 就像是一个通道,在.net运行时和文件之间建立一个桥梁, 然后通过StreamReader读取数据。
      

  4.   

    msdn上的代码果然很牛x
    streamreader。BaseStream就是操作streamreader的,返回的结果就是streamreader
      

  5.   

    StreamReader是以一种特定的编码输入字符带缓冲的流字符读取器,而基础流就是它读取的未指定编码的无缓冲的字节流...因此StreamReader可能缓冲输入使基础流的位置与StreamReader的位置不匹配,字符流的长度和基础流的字节长度也不见得相同...