Share via


方法 : ログ ファイルを開いて情報を追加する

StreamWriterStreamReader は、文字をストリームに書き込んだり、文字をストリームから読み取ったりします。 入力用として log.txt ファイルを開くか、ファイルが存在しない場合は作成し、ファイルの末尾に情報を追加するコードの例を次に示します。 その後で、ファイルの内容を表示するために標準出力に書き込みます。 この例に代わる方法として、情報を 1 つの文字列または文字列配列として格納できます。また、WriteAllText または WriteAllLines メソッドを使用して同じ機能を実行することもできます。

メモメモ

Visual Basic では、ログ ファイルの作成、またはログ ファイルへの書き込みを行う場合に、Log クラスまたは FileSystem クラスに用意されているメソッドとプロパティを使用することもできます。

使用例

Imports System
Imports System.IO

Class DirAppend
    Public Shared Sub Main()
        Using w As StreamWriter = File.AppendText("log.txt")
            Log("Test1", w)
            Log("Test2", w)
            ' Close the writer and underlying file.
            w.Close()
        End Using
        ' Open and read the file.
        Using r As StreamReader = File.OpenText("log.txt")
            DumpLog(r)
        End Using
    End Sub

    Public Shared Sub Log(logMessage As String, w As TextWriter)
        w.Write(vbCrLf + "Log Entry : ")
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), _
            DateTime.Now.ToLongDateString())
        w.WriteLine("  :")
        w.WriteLine("  :{0}", logMessage)
        w.WriteLine ("-------------------------------")
        ' Update the underlying file.
        w.Flush()
    End Sub

    Public Shared Sub DumpLog(r As StreamReader)
        ' While not at the end of the file, read and write lines.
        Dim line As String
        line = r.ReadLine()
        While Not (line Is Nothing)
            Console.WriteLine(line)
            line = r.ReadLine()
        End While
        r.Close()
    End Sub
End Class
using System;
using System.IO;

class DirAppend
{
    public static void Main()
    {
        using (StreamWriter w = File.AppendText("log.txt"))
        {
            Log("Test1", w);
            Log("Test2", w);
            // Close the writer and underlying file.
            w.Close();
        }
        // Open and read the file.
        using (StreamReader r = File.OpenText("log.txt"))
        {
            DumpLog(r);
        }
    }

    public static void Log(string logMessage, TextWriter w)
    {
        w.Write("\r\nLog Entry : ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        w.WriteLine("  :");
        w.WriteLine("  :{0}", logMessage);
        w.WriteLine ("-------------------------------");
        // Update the underlying file.
        w.Flush();
    }

    public static void DumpLog(StreamReader r)
    {
        // While not at the end of the file, read and write lines.
        string line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
        r.Close();
    }
}
using namespace System;
using namespace System::IO;

ref class DirAppend
{
public:
    static void Main()
    {
        StreamWriter^ w = File::AppendText("log.txt");
        Log("Test1", w);
        Log("Test2", w);
        // Close the writer and underlying file.
        w->Close();
        // Open and read the file.
        StreamReader^ r = File::OpenText("log.txt");
        DumpLog(r);
        r->Close();
    }

    static void Log(String^ logMessage, TextWriter^ w)
    {
        w->Write("\r\nLog Entry : ");
        w->WriteLine("{0} {1}", DateTime::Now.ToLongTimeString(),
            DateTime::Now.ToLongDateString());
        w->WriteLine("  :");
        w->WriteLine("  :{0}", logMessage);
        w->WriteLine ("-------------------------------");
        // Update the underlying file.
        w->Flush();
    }

    static void DumpLog(StreamReader^ r)
    {
        // While not at the end of the file, read and write lines.
        String^ line;
        while ((line = r->ReadLine()) != nullptr)
        {
            Console::WriteLine(line);
        }
        r->Close();
    }
};

int main()
{
    DirAppend::Main();
}

参照

処理手順

方法 : ディレクトリ一覧を作成する

方法 : 新しく作成されたデータ ファイルに対して読み書きする

方法 : ファイルからテキストを読み取る

方法 : ファイルにテキストを書き込む

方法 : 文字列から文字を読み取る

方法 : 文字列に文字を書き込む

参照

StreamWriter

StreamReader

File.AppendText

File.OpenText

StreamReader.ReadLine

概念

基本のファイル I/O