Share via


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

업데이트: 2007년 11월

StreamWriterStreamReader는 각각 스트림에 문자를 쓰고 스트림에서 문자를 읽습니다. 다음 코드 예제는 입력에 사용할 log.txt 파일을 열거나 이 파일이 없는 경우에는 새로 만들고 파일의 끝에 정보를 추가합니다. 그런 다음 표시할 표준 출력에 파일 내용을 기록합니다. 이 예제의 정보는 단일 문자열이나 문자열 배열로 저장할 수 있고 WriteAllText 또는 WriteAllLines 메서드를 사용하여 같은 기능을 얻을 수 있습니다.

참고:

Visual Basic 사용자는 로그 파일에 대한 만들기 또는 쓰기에 대해 My.Application.Log 또는 My.Computer.FileSystem 개체에서 제공하는 메서드와 속성을 사용하도록 선택할 수 있습니다. 자세한 내용은 My.Application.Log 개체My.Computer.FileSystem 개체를 참조하십시오.

예제

Option Explicit On 
Option Strict On
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
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(ByVal logMessage As String, ByVal w As TextWriter)
        w.Write(ControlChars.CrLf & "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(ByVal 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(String[] args)
    {
        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();
    }
}

참고 항목

작업

방법: 디렉터리 목록 만들기

방법: 새로 만든 데이터 파일 읽기 및 쓰기

방법: 파일의 텍스트 읽기

방법: 파일에 텍스트 쓰기

방법: 문자열에서 문자 읽기

방법: 문자열에 문자 쓰기

개념

기본 파일 I/O

참조

StreamWriter

StreamReader

File.AppendText

File.OpenText

StreamReader.ReadLine