diff --git a/Boats/Boats/MapsCollection.cs b/Boats/Boats/MapsCollection.cs index 8405f35..d818b99 100644 --- a/Boats/Boats/MapsCollection.cs +++ b/Boats/Boats/MapsCollection.cs @@ -120,47 +120,41 @@ namespace Boats { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = File.OpenText(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string? currentLine = sr.ReadLine(); + if (currentLine == null || !currentLine.Contains("MapsCollection")) { - bufferTextFromFile += temp.GetString(b); + //если нет такой записи, то это не те данные + return false; } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (!strs[0].Contains("MapsCollection")) - { - //если нет такой записи, то это не те данные - return false; - } - //очищаем записи - _mapStorages.Clear(); - for (int i = 1; i < strs.Length; ++i) - { - var elem = strs[i].Split(separatorDict); - AbstractMap map = null; - switch (elem[1]) + //очищаем записи + _mapStorages.Clear(); + currentLine = sr.ReadLine(); + while (currentLine != null) { - case "SimpleMap": - map = new SimpleMap(); - break; - case "OceanMap": - map = new OceanMap(); - break; - case "LineMap": - map = new LineMap(); - break; + var elem = currentLine.Split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "OceanMap": + map = new OceanMap(); + break; + case "LineMap": + map = new LineMap(); + break; + } + _mapStorages.Add( + elem[0], + new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map) + ); + _mapStorages[elem[0]].LoadData( + elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); + currentLine = sr.ReadLine(); } - _mapStorages.Add( - elem[0], - new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map) - ); - _mapStorages[elem[0]].LoadData( - elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } return true; }