StreamReader.Peek StreamReader.Peek StreamReader.Peek StreamReader.Peek Method

定義

傳回下一個可供使用的字元,但不使用它。Returns the next available character but does not consume it.

public:
 override int Peek();
public override int Peek ();
override this.Peek : unit -> int
Public Overrides Function Peek () As Integer

傳回

整數,表示要讀取的下一個字元,如果沒有要讀取的字元或資料流不支援搜尋,則為 -1。An integer representing the next character to be read, or -1 if there are no characters to be read or if the stream does not support seeking.

例外狀況

發生 I/O 錯誤。An I/O error occurs.

範例

下列程式碼範例會從檔案讀取行,直到到達檔案結尾為止。The following code example reads lines from a file until the end of the file is reached.

using namespace System;
using namespace System::IO;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }
      StreamWriter^ sw = gcnew StreamWriter( path );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      StreamReader^ sr = gcnew StreamReader( path );
      try
      {
         while ( sr->Peek() > -1 )
         {
            Console::WriteLine( sr->ReadLine() );
         }
      }
      finally
      {
         delete sr;
      }
   }
   catch ( Exception^ e )
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test 
{
    
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        try 
        {
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path)) 
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path)) 
            {

                while (sr.Peek() > -1) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO
Imports System.Text

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() > -1
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

備註

Peek方法會傳回整數值,以判斷檔案結尾或是否發生其他錯誤。The Peek method returns an integer value in order to determine whether the end of the file, or another error has occurred. 這可讓使用者先檢查傳回的值是否為-1,再將它轉換成Char類型。This allows a user to first check if the returned value is -1 before casting it to a Char type.

這個方法覆寫 TextReader.PeekThis method overrides TextReader.Peek.

StreamReader物件的目前位置不會由Peek變更。The current position of the StreamReader object is not changed by Peek.

適用於

另請參閱