public static void SaveFile(string fileName, Path3D path) { using (StreamWriter writer = new StreamWriter(fileName)) { writer.Write(path); } }
public static Path3D LoadFile(string fileName) { Path3D path = new Path3D(); using (StreamReader sr = new StreamReader(fileName)) { string readFile = sr.ReadToEnd(); string pattern = "(\\d+[\\.{1}\\d+]*).[^\\.\\d]*(\\d+[\\.{1}\\d+]*).[^\\.\\d]*(\\d+[\\.{1}\\d+]*)"; var reg = new Regex(pattern); var matchs = reg.Matches(readFile); if (matchs.Count <= 0) { throw new ApplicationException("Invalid data in file " + fileName); } foreach (Match match in matchs) { double x = double.Parse(match.Groups[1].Value); double y = double.Parse(match.Groups[2].Value); double z = double.Parse(match.Groups[3].Value); Point3D p = new Point3D(x, y, z); path.AddPoint(p); } } return(path); }
public static void LoadPathFromFile(string inputFileLocation) { StreamReader reader = null; StreamWriter writer = null; try { reader = new StreamReader(inputFileLocation, Encoding.GetEncoding("windows-1251")); } catch (IOException) { throw new IOException("Unable to create output file"); } try { writer = new StreamWriter(@"..\..\Paths.txt"); } catch (IOException) { throw new IOException("Unable to create output file"); } int counter = 0; string s; using (reader) using (writer) { while (true) { s = reader.ReadLine(); if (s == null) { break; } var pointsInPath = (from Match match in Regex.Matches(s, PointMatcher) let xCoordinate = double.Parse(match.Groups[1].Value) let yCoordinate = double.Parse(match.Groups[2].Value) let zCoordinate = double.Parse(match.Groups[3].Value) select new Point3D(xCoordinate, yCoordinate, zCoordinate)).ToList(); if (pointsInPath.Count <= 0) { continue; } counter++; var pathFromFile = new Path3D(pointsInPath); writer.WriteLine("Path {0}: {1}", counter, pathFromFile); } } }
public static void LoadPathFromFile(string inputFileLocation) { StreamReader reader = null; StreamWriter writer = null; try { reader = new StreamReader(inputFileLocation, Encoding.GetEncoding("windows-1251")); } catch (IOException) { throw new IOException("Unable to create output file"); } try { writer = new StreamWriter(@"..\..\Paths.txt"); } catch (IOException) { throw new IOException("Unable to create output file"); } int counter = 0; string s; using (reader) using (writer) { while (true) { s = reader.ReadLine(); if (s == null) { break; } var pointsInPath = (from Match match in Regex.Matches(s, PointMatcher) let xCoordinate = double.Parse(match.Groups[1].Value) let yCoordinate = double.Parse(match.Groups[2].Value) let zCoordinate = double.Parse(match.Groups[3].Value) select new Point3D(xCoordinate, yCoordinate, zCoordinate)).ToList(); if (pointsInPath.Count <= 0) continue; counter++; var pathFromFile = new Path3D(pointsInPath); writer.WriteLine("Path {0}: {1}", counter, pathFromFile); } } }
static void Main(string[] args) { Point3D p1 = new Point3D(1, 2, 3); Point3D p2 = new Point3D(3, 4.66, 5.55); Point3D p3 = new Point3D(5.6, 4.3, 2.2); Path3D path = new Path3D(p1, p2, p3); Console.WriteLine("Save path:\n{0}", path); Storage.SaveFile(".../.../Save.txt", path); Path3D loadPath = Storage.LoadFile(".../.../Load.txt"); Console.WriteLine("Load path:\n{0}", loadPath); }