diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs index 34b004b..63b6d35 100644 --- a/Battleship/Battleship/ShipsGenericStorage.cs +++ b/Battleship/Battleship/ShipsGenericStorage.cs @@ -75,8 +75,7 @@ namespace Battleship.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _shipStorages) + foreach (KeyValuePair> record in _shipStorages) { StringBuilder records = new(); foreach (DrawningShip? elem in record.Value.GetShips) @@ -89,10 +88,10 @@ namespace Battleship.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using (StreamWriter writer = new StreamWriter(filename)) + { + writer.Write($"ShipStorage{Environment.NewLine}{data}"); + } return true; } /// @@ -107,53 +106,51 @@ namespace Battleship.Generics { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + + using (StreamReader fs = File.OpenText(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = fs.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; } - ShipGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + + _shipStorages.Clear(); + string strs = ""; + + while ((strs = fs.ReadLine()) != null) { - DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); - if (ship != null) + if (strs == null) { - if (!(collection + ship)) + return false; + } + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + ShipGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawningShip? plane = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight); + if (plane != null) { - return false; + if (!(collection + plane)) + { + return false; + } } } + _shipStorages.Add(record[0], collection); } - _shipStorages.Add(record[0], collection); + return true; } - return true; } } }