public static void Main() { string path = @"..\..\..\MyTest.txt"; int N = 10; // Количество создаваемых объектов (число строк в файле) using (StreamWriter writer = new StreamWriter(path)) { ColorPoint one; for (int i = 0; i < N; i++) { int j = gen.Next(0, ColorPoint.colors.Length); one = new ColorPoint { x = gen.NextDouble(), y = gen.NextDouble(), color = ColorPoint.colors[j] }; writer.WriteLine(one.ToString()); } } Console.WriteLine("Записаны {0} строк в текстовый файл: \n{1}", N, path); }
public static void Main() { string path = @"../../../MyTest.txt"; int N = 10; List <ColorPoint> list = new List <ColorPoint>(); ColorPoint one; for (int i = 0; i < N; i++) { one = new ColorPoint(); one.x = gen.NextDouble(); one.y = gen.NextDouble(); int j = gen.Next(0, ColorPoint.colors.Length); one.color = ColorPoint.colors[j]; list.Add(one); } string[] arrData = Array.ConvertAll(list.ToArray(), (ColorPoint cp) => cp.ToString()); // Запись массива стpок в текстовый файл: File.WriteAllLines(path, arrData); Console.WriteLine("Записаны {0} строк в текстовый файл: \n{1}", N, path); Console.ReadKey(); }
public static void Main() { string path = @"..\..\..\..\MyTest.txt"; int N = int.Parse(Console.ReadLine()); List <ColorPoint> list = new List <ColorPoint>(); ColorPoint one; for (int i = 0; i < N; i++) { one = new ColorPoint(); one.x = gen.NextDouble(); one.y = gen.NextDouble(); int j = gen.Next(0, ColorPoint.colors.Length); one.color = ColorPoint.colors[j]; list.Add(one); } using (BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate))) { foreach (ColorPoint cp in list) { bw.Write(cp.color); bw.Write(cp.x); bw.Write(cp.y); } } Console.WriteLine("Записаны {0} строк в текстовый файл: \n{1}", N, path); using (BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open))) { while (br.BaseStream.Position < br.BaseStream.Length) { ColorPoint cp = new ColorPoint(); cp.color = br.ReadString(); cp.x = br.ReadDouble(); cp.y = br.ReadDouble(); Console.WriteLine(cp); } } }