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

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 Lab1ContainersShip.DrawingObjects;
using Lab1ContainersShip.MovementStrategy;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Lab1ContainersShip
@ -131,10 +132,10 @@ public bool SaveData(string filename)
{
return false;
}
using FileStream fs = new FileStream(filename, FileMode.Create);
byte[] info = new
UTF8Encoding(true).GetBytes($"ShipStorage{Environment.NewLine}{data}");
fs.Write(info, 0, info.Length);
using(StreamWriter sr = new StreamWriter(filename))
{
sr.Write($"ShipStorage{Environment.NewLine}{data}");
}
return true;
}
/// <summary>
@ -149,54 +150,45 @@ public bool SaveData(string filename)
{
return false;
}
string bufferTextFromFile = "";
using (FileStream fs = new FileStream(filename, FileMode.Open))
using (StreamReader sr = new StreamReader(filename))
{
byte[] b = new byte[fs.Length];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
string str = sr.ReadLine();
if (str == null || str.Length == 0)
{
bufferTextFromFile += temp.GetString(b);
return false;
}
}
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)
if (!str.StartsWith("ShipStorage"))
{
continue;
return false;
}
ShipGenericCollection<DrawingShip, DrawningObjectShip>collection = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
_shipStorages.Clear();
str = sr.ReadLine();
while( str!= null && str.Length != 0)
{
DrawingShip ship =
elem?.CreateDrawingShip(_separatorForObject, _pictureWidth, _pictureHeight);
if (ship != null)
string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
str = sr.ReadLine();
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;
}
}
}