diff --git a/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs b/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs index 4f72e9d..f269f64 100644 --- a/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs +++ b/Lab1ContainersShip/Lab1ContainersShip/ShipGenericStorage.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using System.Xml.Linq; using Lab1ContainersShip.DrawingObjects; using Lab1ContainersShip.MovementStrategy; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace Lab1ContainersShip @@ -131,10 +132,10 @@ public bool SaveData(string filename) { return false; } - using FileStream fs = new FileStream(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using(StreamWriter sr = new StreamWriter(filename)) + { + sr.Write($"ShipStorage{Environment.NewLine}{data}"); + } return true; } /// @@ -149,54 +150,45 @@ public bool SaveData(string filename) { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new FileStream(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new UTF8Encoding(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = sr.ReadLine(); + if (str == null || str.Length == 0) { - bufferTextFromFile += temp.GetString(b); + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("ShipStorage")) - { - //если нет такой записи, то это не те данные - return false; - } - _shipStorages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) + if (!str.StartsWith("ShipStorage")) { - continue; + return false; } - ShipGenericCollectioncollection = new ShipGenericCollection(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords,StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + _shipStorages.Clear(); + str = sr.ReadLine(); + while( str!= null && str.Length != 0) { - DrawingShip ship = - elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight); - if (ship != null) + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + str = sr.ReadLine(); + if (record.Length != 2) { - if (collection + ship == -1) + continue; + } + ShipGenericCollection collection = new ShipGenericCollection(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set.Reverse()) + { + DrawingShip ship = + elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight); + if (ship != null) { - return false; + if (collection + ship == -1) + { + return false; + } } } + _shipStorages.Add(record[0], collection); } - _shipStorages.Add(record[0], collection); } return true; } - - } }