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

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,38 +150,30 @@ 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)
{
bufferTextFromFile += temp.GetString(b);
}
}
var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
string str = sr.ReadLine();
if (str == null || str.Length == 0)
{
return false;
}
if (!strs[0].StartsWith("ShipStorage"))
if (!str.StartsWith("ShipStorage"))
{
//если нет такой записи, то это не те данные
return false;
}
_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)
{
continue;
}
ShipGenericCollection<DrawingShip, DrawningObjectShip>collection = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
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);
@ -194,9 +187,8 @@ public bool SaveData(string filename)
}
_shipStorages.Add(record[0], collection);
}
}
return true;
}
}
}