컴퓨터/C#
[C#] Text 파일 줄 단위 읽기, 쓰기 (System.IO.File)
dolhim
2015. 8. 31. 11:48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | using System.IO; private void LoadIPFile(string fpath) { var AllLines = (string[])null; var numberOfLines = 0; var readLines = 0; var ipList = new List<Point>(); if (!File.Exists(fpath)) return; if (Path.GetExtension(fpath) != ".ip") return; numberOfLines = File.ReadLines(fpath).Count(); AllLines = new string[numberOfLines]; using (var sr = File.OpenText(fpath)) { sr.BaseStream.Position = 0; readLines = 0; while (!sr.EndOfStream) { AllLines[readLines] = sr.ReadLine(); ++readLines; } } for (int i = 0; i < readLines; ++i) { var words = AllLines[i].Split(new Char[] { '\t', ' ' }); if (words.Length == 4) { var ip = new Point(); ip.x = Convert.ToSingle(words[2]); ip.y = Convert.ToSingle(words[3]); ipList.Add(ip); } else { Console.WriteLine("Error : ip 파일 형식이 잘못되었습니다. {0}, 줄 {1}", Path.GetFileName(fpath), readLines); } } this.originGCPs = ipList; } | cs |
| using System.IO; private bool SavePtsFile(string path, List<Point> pts) { using (var sw = new StreamWriter(path)) { foreach (var pt in pts) { var line = string.Format("{0}\t{1}", pt.X, pt.Y); sw.WriteLine(line); } } return true; } | cs |