StreamReader.Peek Méthode

Définition

Retourne le caractère disponible suivant, mais ne le consomme pas.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

Retours

Int32

Entier représentant le caractère suivant à lire, ou -1 s'il n'y a pas de caractère à lire ou si le flux ne prend pas en charge la recherche.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.

Exceptions

Une erreur d’E/S se produit.An I/O error occurs.

Exemples

L’exemple de code suivant lit des lignes à partir d’un fichier jusqu’à ce que la fin du fichier soit atteinte.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

Remarques

La Peek méthode retourne une valeur entière pour déterminer si la fin du fichier ou une autre erreur s’est produite.The Peek method returns an integer value in order to determine whether the end of the file, or another error has occurred. Cela permet à un utilisateur de vérifier d’abord si la valeur retournée est-1 avant de la convertir en un Char type.This allows a user to first check if the returned value is -1 before casting it to a Char type.

Cette méthode se substitue à TextReader.Peek.This method overrides TextReader.Peek.

La position actuelle de l' StreamReader objet n’est pas modifiée par Peek .The current position of the StreamReader object is not changed by Peek.

S’applique à

Voir aussi