File.CreateText(String) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
UTF-8로 인코딩된 텍스트를 쓰기 위한 파일을 만들거나 엽니다. 파일이 이미 있는 경우 해당 내용이 바뀝니다.
public:
static System::IO::StreamWriter ^ CreateText(System::String ^ path);
public static System.IO.StreamWriter CreateText(string path);
static member CreateText : string -> System.IO.StreamWriter
Public Shared Function CreateText (path As String) As StreamWriter
매개 변수
- path
- String
쓰기 위해 열 파일입니다.
반품
StreamWriter UTF-8 인코딩을 사용하여 지정된 파일에 쓰는 A입니다.
예외
호출자에게 필요한 권한이 없습니다.
-또는-
path 가 읽기 전용인 파일을 지정했습니다.
-또는-
path 는 숨겨진 파일을 지정했습니다.
.NET Framework 및 .NET Core 버전 2.1 이전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. 메서드를 사용하여 잘못된 문자를 쿼리할 GetInvalidPathChars() 수 있습니다.
path은 null입니다.
지정된 경로, 파일 이름 또는 둘 다 시스템 정의 최대 길이를 초과합니다.
지정한 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있는 경우).
path 가 잘못된 형식입니다.
예제
다음 예제에서는 텍스트 쓰기 및 읽기용 파일을 만듭니다.
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
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");
}
}
// 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"
if File.Exists path |> not then
// Create a file to write to.
use sw = File.CreateText path
sw.WriteLine "Hello"
sw.WriteLine "Welcome"
// 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
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
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
' 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) .로 설정된 append생성자 오버로드와 false 동일합니다. 지정한 path 파일이 없으면 만들어집니다. 파일이 있으면 해당 내용이 바뀝니다. 열려 있는 동안 추가 스레드가 파일을 읽을 수 있습니다.
path 매개 변수는 상대 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 다음을 참조하세요 GetCurrentDirectory.
일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.