StreamReader.Peek Metode

Definisi

Mengembalikan karakter berikutnya yang tersedia tetapi tidak menggunakannya.

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

Mengembalikan

Bilangan bulat yang mewakili karakter berikutnya yang akan dibaca, atau -1 jika tidak ada karakter yang akan dibaca atau jika aliran tidak mendukung pencarian.

Pengecualian

Terjadi kesalahan I/O.

Contoh

Contoh kode berikut membaca baris dari file hingga akhir file tercapai.

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

Keterangan

Metode mengembalikan Peek nilai bilangan bulat untuk menentukan apakah akhir file, atau kesalahan lain telah terjadi. Ini memungkinkan pengguna untuk terlebih dahulu memeriksa apakah nilai yang dikembalikan adalah -1 sebelum mentransmisikannya ke Char jenis.

Metode ini mengambil alih TextReader.Peek.

Posisi StreamReader objek saat ini tidak diubah oleh Peek.

Berlaku untuk

Lihat juga