чтение через StreamReader

This commit is contained in:
Nikita Potapov 2022-11-26 15:24:41 +04:00
parent f478227337
commit 8062309a2c

View File

@ -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<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map)
);
_mapStorages[elem[0]].LoadData(
elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
currentLine = sr.ReadLine();
}
_mapStorages.Add(
elem[0],
new MapWithSetBoatsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map)
);
_mapStorages[elem[0]].LoadData(
elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
return true;
}