изменение на StreamWriter и StreamReader

This commit is contained in:
Inohara 2022-11-16 09:56:28 +04:00
parent a01ca36876
commit c881f02fa8

View File

@ -94,12 +94,12 @@ namespace AircraftCarrier
{ {
File.Delete(filename); File.Delete(filename);
} }
using (FileStream fs = new(filename, FileMode.Create)) using (StreamWriter sw = new(filename))
{ {
WriteToFile($"MapsCollection{Environment.NewLine}", fs); sw.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages) foreach (var storage in _mapStorages)
{ {
WriteToFile($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}", fs); sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
} }
} }
return true; return true;
@ -116,39 +116,30 @@ namespace AircraftCarrier
{ {
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader sr = new(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string str = "";
UTF8Encoding temp = new(true); if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
while (fs.Read(b, 0, b.Length) > 0)
{ {
bufferTextFromFile += temp.GetString(b); return false;
} }
} _mapStorages.Clear();
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); while ((str = sr.ReadLine()) != null)
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])
{ {
case "Простая карта": var elem = str.Split(separatorDict);
map = new SimpleMap(); AbstractMap map = null;
break; switch (elem[1])
case "Преграды-линии": {
map = new LineMap(); case "SimpleMap":
break; map = new SimpleMap();
break;
case "Преграды-линии":
map = new LineMap();
break;
}
_mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
_mapStorages.Add(elem[0], new MapWithSetWarshipsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
return true; return true;
} }