Vb.net-text-files

提供:Dev Guides
移動先:案内検索

テキストファイルの読み取りと書き込み

*StreamReader* および *StreamWriter* クラスは、テキストファイルの読み取りと書き込みに使用されます。 これらのクラスは、ファイルストリームへのバイトの読み取りと書き込みをサポートする抽象基本クラスStreamを継承します。

StreamReaderクラス

*StreamReader* クラスは、一連の文字を読み取るためのリーダーを表す抽象基本クラスTextReaderからも継承します。 次の表は、StreamReaderクラスで一般的に使用される*メソッド*の一部を示しています-
Sr.No. Method Name & Purpose
1

Public Overrides Sub Close

StreamReaderオブジェクトと基になるストリームを閉じ、リーダーに関連付けられているシステムリソースを解放します。

2

Public Overrides Function Peek As Integer

次の使用可能な文字を返しますが、それを消費しません。

3

Public Overrides Function Read As Integer

入力ストリームから次の文字を読み取り、文字位置を1文字進めます。

次の例は、Jamaica.txtという名前のテキストファイルの読み取りを示しています。 ファイルの読み取り-

Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop
Imports System.IO
Module fileProg
   Sub Main()
      Try
         ' Create an instance of StreamReader to read from a file.
         ' The using statement also closes the StreamReader.
         Using sr As StreamReader = New StreamReader("e:/jamaica.txt")
            Dim line As String
            ' Read and display lines from the file until the end of
            ' the file is reached.
            line = sr.ReadLine()
            While (line <> Nothing)
               Console.WriteLine(line)
               line = sr.ReadLine()
            End While
         End Using
      Catch e As Exception
         ' Let the user know what went wrong.
         Console.WriteLine("The file could not be read:")
         Console.WriteLine(e.Message)
      End Try
      Console.ReadKey()
   End Sub
End Module

プログラムをコンパイルして実行したときに表示されるものを推測してください!

StreamWriterクラス

*StreamWriter* クラスは、一連の文字を書き込むことができるライターを表す抽象クラスTextWriterを継承します。

次の表は、このクラスの最も一般的に使用されるメソッドの一部を示しています-

Sr.No. Method Name & Purpose
1

Public Overrides Sub Close

現在のStreamWriterオブジェクトと基になるストリームを閉じます。

2

Public Overrides Sub Flush

現在のライターのすべてのバッファをクリアし、バッファされたデータを基になるストリームに書き込みます。

3

Public Overridable Sub Write (value As Boolean)

ブール値のテキスト表現をテキスト文字列またはストリームに書き込みます。 (TextWriterから継承されます。)

4

Public Overrides Sub Write (value As Char)

文字をストリームに書き込みます。

5

Public Overridable Sub Write (value As Decimal)

10進数値のテキスト表現をテキスト文字列またはストリームに書き込みます。

6

Public Overridable Sub Write (value As Double)

8バイトの浮動小数点値のテキスト表現をテキスト文字列またはストリームに書き込みます。

7

Public Overridable Sub Write (value As Integer)

4バイトの符号付き整数のテキスト表現をテキスト文字列またはストリームに書き込みます。

8

Public Overrides Sub Write (value As String)

文字列をストリームに書き込みます。

9

Public Overridable Sub WriteLine

行終端文字をテキスト文字列またはストリームに書き込みます。

上記のリストは完全なものではありません。 メソッドの完全なリストについては、Microsoftのドキュメントをご覧ください。

次の例は、StreamWriterクラスを使用してファイルにテキストデータを書き込む方法を示しています-

Imports System.IO
Module fileProg
   Sub Main()
      Dim names As String() = New String() {"Zara Ali", _
         "Nuha Ali", "Amir Sohel", "M Amlan"}
      Dim s As String
      Using sw As StreamWriter = New StreamWriter("names.txt")
         For Each s In names
            sw.WriteLine(s)
         Next s
      End Using
      ' Read and show each line from the file.
      Dim line As String
      Using sr As StreamReader = New StreamReader("names.txt")
         line = sr.ReadLine()
         While (line <> Nothing)
            Console.WriteLine(line)
            line = sr.ReadLine()
         End While
      End Using
      Console.ReadKey()
   End Sub
End Module

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Zara Ali
Nuha Ali
Amir Sohel
M Amlan