File.AppendText(String) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
StreamWriter 지정된 파일이 없는 경우 UTF-8로 인코딩된 텍스트를 기존 파일 또는 새 파일에 추가하는 파일을 만듭니다.
public:
static System::IO::StreamWriter ^ AppendText(System::String ^ path);
public static System.IO.StreamWriter AppendText(string path);
static member AppendText : string -> System.IO.StreamWriter
Public Shared Function AppendText (path As String) As StreamWriter
매개 변수
- path
- String
추가할 파일의 경로입니다.
반품
UTF-8로 인코딩된 텍스트를 지정된 파일 또는 새 파일에 추가하는 스트림 작성기입니다.
예외
호출자에게 필요한 권한이 없습니다.
.NET Framework 및 .NET Core 버전 2.1 이전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. 메서드를 사용하여 잘못된 문자를 쿼리할 GetInvalidPathChars() 수 있습니다.
path은 null입니다.
지정된 경로, 파일 이름 또는 둘 다 시스템 정의 최대 길이를 초과합니다.
지정한 경로가 잘못되었습니다(예: 디렉터리가 없거나 매핑되지 않은 드라이브에 있음).
path 가 잘못된 형식입니다.
예제
다음 예제에서는 파일에 텍스트를 추가합니다. 파일이 없는 경우 메서드는 새 파일을 만듭니다. 그러나 예제를 성공적으로 완료하려면 C 드라이브에 이름이 지정된 temp 디렉터리가 있어야 합니다.
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
open System.IO
let path = @"c:\temp\MyTest.txt"
// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
use sw = File.CreateText path
sw.WriteLine "Hello"
sw.WriteLine "And"
sw.WriteLine "Welcome"
// This text is always added, making the file longer over time
// if it is not deleted.
do
use sw = File.AppendText path
sw.WriteLine "This"
sw.WriteLine "is Extra"
sw.WriteLine "Text"
// Open the file to read from.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()
while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' This text is added only once to the file.
If Not File.Exists(path) Then
' Create a file to write to.
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
End Using
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Using sw As StreamWriter = File.AppendText(path)
sw.WriteLine("This")
sw.WriteLine("is Extra")
sw.WriteLine("Text")
End Using
' Open the file to read from.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
End Sub
End Class
설명
이 메서드는 생성자 오버로드와 동일합니다 StreamWriter(String, Boolean) . 지정한 path 파일이 없으면 만들어집니다. 파일이 있는 경우 파일에 텍스트를 추가하는 작업에 작업을 StreamWriter 씁니다. 열려 있는 동안 추가 스레드가 파일을 읽을 수 있습니다.
path 매개 변수는 상대 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 다음을 참조하세요 GetCurrentDirectory.
path 매개 변수는 대/소문자를 구분하지 않습니다.
일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.