стрим ридер и райтер

This commit is contained in:
AnnaLioness 2023-11-20 14:52:10 +04:00
parent 21cf14be5b
commit 256a3c4bf4

View File

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