방법: 로그 파일 열기 및 추가

StreamWriterStreamReader는 각각 스트림에 문자를 쓰고 스트림에서 문자를 읽습니다. 다음 코드 예제는 입력에 사용할 log.txt 파일을 열거나 이 파일이 없는 경우에는 새로 만들고 파일의 끝에 정보를 추가합니다. 그런 다음 표시할 표준 출력에 파일 내용을 기록합니다. 이 예제의 정보는 단일 문자열이나 문자열 배열로 저장할 수 있고 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