[C#] Lec 12 - 파일 입출력
[C#] Lec 12 - 파일 입출력
파일 다루기
- File 클래스 이용하여 진행 (system.io)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.IO;
string fileName = @"C:\Users\SyDLab\Documents\햇빛반키.csv";
if (File.Exists(fileName))
{
using (StreamReader sr = new StreamReader(fileName)) //FileStream은 닫아줘야 하므로 Using 사용
{
while(!sr.EndOfStream )
{
string line = sr.ReadLine();
string[] token = line.Split(",".ToCharArray(), StringSplitOption.RemoveEmptyEntries);
profiles.Add(new Profile() {Name = token[0], Height = int.Parse(token[1])} );
}
sr.Close();
}
}
- 데이터를 다시 파일화
1
2
3
4
5
6
7
8
9
10
11
12
if (!File.Exists(fileName))
{
using (StreamWriter sw = new StreamWriter(fileName, false, System.Text.Encoding.UTF8))
{
foreach (var item in profiles3) {
string line = item.Name+","+item.Height.ToString();
sw.WriteLine(line);
}
sw.Close();
}
}
절대 경로, 상대 경로
1
2
string fileName = System.IO.Path.GetFullPath(@"..\..\..\"); // .. : 상위 디렉토리로
fileName += @"\Data\햇빛반키.csv";
연산자 재정의
- +, -, !, ~, ++, –, true, false : 단항 연산자 오버로드 가능
+, -, *, /, %, &, , ^, «, » : 이항 연산자 오버로드 가능 - ==, !=, <, > , <=, >= : 비교 연산자 오버로드 가능
&&, : 조건 논리 연산자는 오버로드 불가능 but 오버로드 가능한 &, 사용해서 계산가능 - [] : 배열 인덱싱 연산자는 오버로드 할 수 없지만 인덱서 정의가능
- x : 오버로드 할 수 없음
+=, -=, *=, /=, %=, &=, =, ^=, «=, »= : 오버로드할 수 없음 - =, . , ?: , ??, →, =>, f(x), as, cheked, unchecked, default, delegate, is, new, sizeof, typeof : 오버로드할 수 없음
- public static OUTPUT operator (OPERATORNAME) (input) {}
This post is licensed under CC BY 4.0 by the author.