File.ReadAllLines 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
텍스트 파일을 열고 파일의 모든 줄을 문자열 배열로 읽은 다음 파일을 닫습니다.
오버로드
| Name | Description |
|---|---|
| ReadAllLines(String) |
텍스트 파일을 열고 파일의 모든 줄을 읽은 다음 파일을 닫습니다. |
| ReadAllLines(String, Encoding) |
파일을 열고 지정된 인코딩을 사용하여 파일의 모든 줄을 읽은 다음 파일을 닫습니다. |
ReadAllLines(String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
텍스트 파일을 열고 파일의 모든 줄을 읽은 다음 파일을 닫습니다.
public:
static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path);
public static string[] ReadAllLines(string path);
static member ReadAllLines : string -> string[]
Public Shared Function ReadAllLines (path As String) As String()
매개 변수
- path
- String
읽기 위해 열 파일입니다.
반품
파일의 모든 줄을 포함하는 문자열 배열입니다.
예외
.NET Framework 및 .NET Core 버전 2.1 이전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. 메서드를 사용하여 잘못된 문자를 쿼리할 GetInvalidPathChars() 수 있습니다.
path은 null입니다.
지정된 경로, 파일 이름 또는 둘 다 시스템 정의 최대 길이를 초과합니다.
지정한 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있는 경우).
파일을 여는 동안 I/O 오류가 발생했습니다.
이 작업은 현재 플랫폼에서 지원되지 않습니다.
-또는-
path 디렉터리를 지정했습니다.
-또는-
호출자에게 필요한 권한이 없습니다.
지정한 path 파일을 찾을 수 없습니다.
path 가 잘못된 형식입니다.
호출자에게 필요한 권한이 없습니다.
예제
다음 코드 예제에서는 파일의 ReadAllLines 내용을 표시 하는 메서드를 사용 하는 방법을 보여 줍니다. 이 예제에서는 파일이 아직 없는 경우 만들어지고 텍스트가 추가됩니다.
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.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
open System
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.
let createText = [ "Hello"; "And"; "Welcome" ]
File.WriteAllLines(path, createText)
// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
"This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
// Open the file to read from.
let readText = File.ReadAllLines path
for s in readText do
printfn $"{s}"
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
설명
이 메서드는 파일을 열고 파일의 각 줄을 읽은 다음 각 줄을 문자열 배열의 요소로 추가합니다. 그런 다음 파일을 닫습니다. 줄은 문자 시퀀스 뒤에 캐리지 리턴('\r'), 줄 바꿈('\n') 또는 줄 바꿈 바로 뒤에 캐리지 리턴으로 정의됩니다. 결과 문자열에는 종료 캐리지 리턴 및/또는 줄 바꿈이 포함되지 않습니다.
파일이 줄 바꿈 시퀀스로 끝나는 경우 배열에 추가 빈 줄이 추가되지 않습니다. 예를 들어 포함하는 파일은 "line1\nline2\n" 포함하는 파일과 동일한 2개 요소 배열(["line1", "line2"])을 생성합니다 "line1\nline2".
이 메서드는 바이트 순서 표시의 존재에 따라 파일의 인코딩을 자동으로 검색하려고 시도합니다. 인코딩 형식 UTF-8 및 UTF-32(big-endian 및 little-endian 모두)를 검색할 수 있습니다.
추가 정보
적용 대상
ReadAllLines(String, Encoding)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
파일을 열고 지정된 인코딩을 사용하여 파일의 모든 줄을 읽은 다음 파일을 닫습니다.
public:
static cli::array <System::String ^> ^ ReadAllLines(System::String ^ path, System::Text::Encoding ^ encoding);
public static string[] ReadAllLines(string path, System.Text.Encoding encoding);
static member ReadAllLines : string * System.Text.Encoding -> string[]
Public Shared Function ReadAllLines (path As String, encoding As Encoding) As String()
매개 변수
- path
- String
읽기 위해 열 파일입니다.
- encoding
- Encoding
파일 내용에 적용되는 인코딩입니다.
반품
파일의 모든 줄을 포함하는 문자열 배열입니다.
예외
.NET Framework 및 .NET Core 버전 2.1 이전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. 메서드를 사용하여 잘못된 문자를 쿼리할 GetInvalidPathChars() 수 있습니다.
path은 null입니다.
지정된 경로, 파일 이름 또는 둘 다 시스템 정의 최대 길이를 초과합니다.
지정한 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있는 경우).
파일을 여는 동안 I/O 오류가 발생했습니다.
이 작업은 현재 플랫폼에서 지원되지 않습니다.
-또는-
path 디렉터리를 지정했습니다.
-또는-
호출자에게 필요한 권한이 없습니다.
지정한 path 파일을 찾을 수 없습니다.
path 가 잘못된 형식입니다.
호출자에게 필요한 권한이 없습니다.
예제
다음 코드 예제에서는 파일의 ReadAllLines 내용을 표시 하는 메서드를 사용 하는 방법을 보여 줍니다. 이 예제에서는 파일이 아직 없는 경우 만들어지고 텍스트가 추가됩니다.
using System;
using System.IO;
using System.Text;
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.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText, Encoding.UTF8);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText, Encoding.UTF8);
// Open the file to read from.
string[] readText = File.ReadAllLines(path, Encoding.UTF8);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
open System
open System.IO
open System.Text
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.
let createText = [ "Hello"; "And"; "Welcome" ]
File.WriteAllLines(path, createText, Encoding.UTF8)
// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
"This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
// Open the file to read from.
let readText = File.ReadAllLines(path, Encoding.UTF8)
for s in readText do
printfn $"{s}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText, Encoding.UTF8)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path, Encoding.UTF8)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
설명
이 메서드는 파일을 열고 파일의 각 줄을 읽은 다음 각 줄을 문자열 배열의 요소로 추가합니다. 그런 다음 파일을 닫습니다. 줄은 문자 시퀀스 뒤에 캐리지 리턴('\r'), 줄 바꿈('\n') 또는 줄 바꿈 바로 뒤에 캐리지 리턴으로 정의됩니다. 결과 문자열에는 종료 캐리지 리턴 및/또는 줄 바꿈이 포함되지 않습니다.
파일이 줄 바꿈 시퀀스로 끝나는 경우 배열에 추가 빈 줄이 추가되지 않습니다. 예를 들어 포함하는 파일은 "line1\nline2\n" 포함하는 파일과 동일한 2개 요소 배열(["line1", "line2"])을 생성합니다 "line1\nline2".
이 메서드는 바이트 순서 표시의 존재에 따라 파일의 인코딩을 자동으로 검색하려고 시도합니다. 인코딩 형식 UTF-8 및 UTF-32(big-endian 및 little-endian 모두)를 검색할 수 있습니다.