Share via


Membaca teks dari file

Contoh berikut menunjukkan cara membaca teks secara sinkron dan asinkron dari file teks menggunakan .NET untuk aplikasi desktop. Dalam kedua contoh tersebut, saat Anda membuat instans kelas StreamReader, Anda dapat menyediakan jalur relatif atau absolut ke file.

Catatan

Contoh kode ini tidak berlaku untuk aplikasi Universal Windows (UWP) karena runtime Windows menyediakan jenis aliran yang berbeda untuk membaca dan menulis ke file. Untuk informasi selengkapnya, lihat UWP bekerja dengan file. Untuk contoh yang menunjukkan cara mengonversi antara aliran .NET Framework dan aliran Runtime Windows, lihat Cara: Mengonversi antara aliran .NET Framework dan aliran runtime Windows.

Prasyarat

  • Buat file teks bernama TestFile.txt di folder yang sama dengan aplikasi.

    Tambahkan beberapa konten ke file teks. Contoh dalam artikel ini menulis konten file teks ke konsol.

Membaca file

Contoh berikut menunjukkan operasi baca sinkron dalam aplikasi konsol. Konten file dibaca dan disimpan dalam variabel string, yang kemudian ditulis ke konsol.

  1. Buat StreamReader instans.
  2. StreamReader.ReadToEnd() Panggil metode dan tetapkan hasilnya ke string.
  3. Tulis output ke konsol.
try
{
    // Open the text file using a stream reader.
    using StreamReader reader = new("TestFile.txt");

    // Read the stream as a string.
    string text = reader.ReadToEnd();

    // Write the text to the console.
    Console.WriteLine(text);
}
catch (IOException e)
{
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}
Try
    ' Open the text file using a stream reader.
    Using reader As New StreamReader("TestFile.txt")

        ' Read the stream as a string.
        Dim text As String = reader.ReadToEnd()

        ' Write the text to the console.
        Console.WriteLine(text)

    End Using
Catch ex As IOException
    Console.WriteLine("The file could not be read:")
    Console.WriteLine(ex.Message)
End Try

Membaca file secara asinkron

Contoh berikut menunjukkan operasi baca asinkron dalam aplikasi konsol. Konten file dibaca dan disimpan dalam variabel string, yang kemudian ditulis ke konsol.

  1. Buat StreamReader instans.
  2. Tunggu StreamReader.ReadToEndAsync() metode dan tetapkan hasilnya ke string.
  3. Tulis output ke konsol.
try
{
    // Open the text file using a stream reader.
    using StreamReader reader = new("TestFile.txt");

    // Read the stream as a string.
    string text = await reader.ReadToEndAsync();

    // Write the text to the console.
    Console.WriteLine(text);
}
catch (IOException e)
{
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}
Try
    ' Open the text file using a stream reader.
    Using reader As New StreamReader("TestFile.txt")

        ' Read the stream as a string.
        Dim text As String = Await reader.ReadToEndAsync()

        ' Write the text to the console.
        Console.WriteLine(text)

    End Using
Catch ex As IOException
    Console.WriteLine("The file could not be read:")
    Console.WriteLine(ex.Message)
End Try