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

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,54 +150,45 @@ 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); return false;
} }
} if (!str.StartsWith("ShipStorage"))
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)
{ {
continue; return false;
} }
ShipGenericCollection<DrawingShip, DrawningObjectShip>collection = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight); _shipStorages.Clear();
string[] set = record[1].Split(_separatorRecords,StringSplitOptions.RemoveEmptyEntries); str = sr.ReadLine();
foreach (string elem in set) while( str!= null && str.Length != 0)
{ {
DrawingShip ship = string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight); str = sr.ReadLine();
if (ship != null) if (record.Length != 2)
{ {
if (collection + ship == -1) continue;
}
ShipGenericCollection<DrawingShip, DrawningObjectShip> collection = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set.Reverse())
{
DrawingShip ship =
elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight);
if (ship != null)
{ {
return false; if (collection + ship == -1)
{
return false;
}
} }
} }
_shipStorages.Add(record[0], collection);
} }
_shipStorages.Add(record[0], collection);
} }
return true; return true;
} }
} }
} }