Share via


EndOfStreamException 类

读操作试图超出流的末尾时引发的异常。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class EndOfStreamException
    Inherits IOException
用法
Dim instance As EndOfStreamException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class EndOfStreamException : IOException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class EndOfStreamException : public IOException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class EndOfStreamException extends IOException
SerializableAttribute 
ComVisibleAttribute(true) 
public class EndOfStreamException extends IOException

备注

EndOfStreamException 使用值为 0x80070026 的 HRESULT COR_E_ENDOFSTREAM。

示例

下面的代码示例说明如何通过在 MemoryStream 类之上使用 BinaryReaderBinaryWriter 两个类,在内存中读写 Double 数据。

Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim i As Integer
        Const upperBound As Integer = 1000

        ' Create random data to write to the stream.
        Dim dataArray(upperBound) As Double
        Dim randomGenerator As New Random()
        For i = 0 To upperBound
            dataArray(i) = 100.1 * randomGenerator.NextDouble()
        Next i

        Dim binWriter As New BinaryWriter(New MemoryStream())
        Try

            ' Write data to the stream.
            Console.WriteLine("Writing data to the stream.")
            
            For i = 0 To upperBound
                binWriter.Write(dataArray(i))
            Next i

            ' Create a reader using the stream from the writer.
            Dim binReader As New BinaryReader(binWriter.BaseStream)

            ' Return to the beginning of the stream.
            binReader.BaseStream.Position = 0

            ' Read and verify the data.
            Try
                Console.WriteLine("Verifying the written data.")
                For i = 0 To upperBound
                    If binReader.ReadDouble() <> dataArray(i) Then
                        Console.WriteLine("Error writing data.")
                        Exit For
                    End If
                Next i
                Console.WriteLine("The data was written and verified.")
            Catch ex As EndOfStreamException
                Console.WriteLine("Error writing data: {0}.", _
                    ex.GetType().Name)
            End Try
        Finally
            binWriter.Close()
        End Try

    End Sub
End Class
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);
                }
            }
        }
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   int i;
   const int arrayLength = 1000;
   
   // Create random data to write to the stream.
   array<double>^dataArray = gcnew array<double>(arrayLength);
   Random^ randomGenerator = gcnew Random;
   for ( i = 0; i < arrayLength; i++ )
   {
      dataArray[ i ] = 100.1 * randomGenerator->NextDouble();

   }
   BinaryWriter^ binWriter = gcnew BinaryWriter( gcnew MemoryStream );
   try
   {
      
      // Write data to the stream.
      Console::WriteLine( "Writing data to the stream." );
      i = 0;
      for ( i = 0; i < arrayLength; i++ )
      {
         binWriter->Write( dataArray[ i ] );

      }
      
      // Create a reader using the stream from the writer.
      BinaryReader^ binReader = gcnew BinaryReader( binWriter->BaseStream );
      
      // Return to the beginning of the stream.
      binReader->BaseStream->Position = 0;
      try
      {
         
         // Read and verify the data.
         i = 0;
         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 );
      }

   }
   finally
   {
      binWriter->Close();
   }

}
import System.*;
import System.IO.*;

class BinaryRW
{   
    public static void main(String[] args)
    {
        int i;
        final 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() ;
        }
        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
        try {
            // 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.
            BinaryReader binReader = 
                new BinaryReader(binWriter.get_BaseStream());
            try {
                try {
                    // Return to the beginning of the stream.
                    binReader.get_BaseStream().set_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().get_Name());
                }
            }
            finally {
                if (binReader != null) {
                    binReader = null;
                }
            }
        }
        finally {
            if (binWriter != null) {
                binWriter = null;
            }
        }
    } //main
} //BinaryRW

继承层次结构

System.Object
   System.Exception
     System.SystemException
       System.IO.IOException
        System.IO.EndOfStreamException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

EndOfStreamException 成员
System.IO 命名空间
Exception

其他资源

处理和引发异常
文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本