Сохранение и запись через StreamWriter и StreamReader
This commit is contained in:
parent
e9e19bd2cd
commit
15565fd27b
@ -1,4 +1,5 @@
|
|||||||
using System.Text;
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
using ProjectAirplaneWithRadar.Drawnings;
|
using ProjectAirplaneWithRadar.Drawnings;
|
||||||
|
|
||||||
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
||||||
@ -105,22 +106,23 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
|||||||
if(File.Exists(filename))
|
if(File.Exists(filename))
|
||||||
File.Delete(filename);
|
File.Delete(filename);
|
||||||
|
|
||||||
StringBuilder sb = new();
|
using FileStream fs = new(filename, FileMode.Create);
|
||||||
|
using StreamWriter sw = new StreamWriter(fs);
|
||||||
sb.Append(_collectionKey);
|
sw.Write(_collectionKey);
|
||||||
foreach(KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
||||||
{
|
{
|
||||||
sb.Append(Environment.NewLine);
|
sw.Write(Environment.NewLine);
|
||||||
|
|
||||||
if (value.Value.Count == 0)
|
if (value.Value.Count == 0)
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
sb.Append(value.Key);
|
sw.Write(value.Key);
|
||||||
sb.Append(_separatorForKeyValue);
|
sw.Write(_separatorForKeyValue);
|
||||||
sb.Append(value.Value.GetCollectionType);
|
sw.Write(value.Value.GetCollectionType);
|
||||||
sb.Append(_separatorForKeyValue);
|
sw.Write(_separatorForKeyValue);
|
||||||
sb.Append(value.Value.MaxCount);
|
sw.Write(value.Value.MaxCount);
|
||||||
sb.Append(_separatorForKeyValue);
|
sw.Write(_separatorForKeyValue);
|
||||||
|
|
||||||
foreach (T? item in value.Value.GetItems())
|
foreach (T? item in value.Value.GetItems())
|
||||||
{
|
{
|
||||||
@ -130,14 +132,10 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append(data);
|
sw.Write(data);
|
||||||
sb.Append(_separatorItems);
|
sw.Write(_separatorItems);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
using FileStream fs = new(filename, FileMode.Create);
|
|
||||||
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
|
|
||||||
fs.Write(info, 0, info.Length);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,59 +151,51 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
string bufferTextFromFile = "";
|
|
||||||
using (FileStream fs = new(filename, FileMode.Open))
|
using (FileStream fs = new(filename, FileMode.Open))
|
||||||
{
|
{
|
||||||
byte[] b = new byte[fs.Length];
|
using StreamReader sr = new StreamReader(fs);
|
||||||
UTF8Encoding temp = new(true);
|
|
||||||
while (fs.Read(b, 0, b.Length) > 0)
|
|
||||||
{
|
|
||||||
bufferTextFromFile += temp.GetString(b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
string str = sr.ReadLine();
|
||||||
if (strs == null || strs.Length == 0)
|
if (str == null || str.Length == 0)
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!strs[0].Equals(_collectionKey))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_storages.Clear();
|
|
||||||
foreach (string data in strs)
|
|
||||||
{
|
|
||||||
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
if (record.Length != 4)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
|
||||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
|
||||||
if (collection == null)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
if (!str.Equals(_collectionKey))
|
||||||
|
|
||||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
foreach (string elem in set)
|
|
||||||
{
|
{
|
||||||
if (elem?.CreateDrawningAirplane() is T airplane)
|
return false;
|
||||||
{
|
|
||||||
if (collection.Insert(airplane) == -1)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
_storages.Clear();
|
||||||
|
|
||||||
_storages.Add(record[0], collection);
|
while (!sr.EndOfStream)
|
||||||
|
{
|
||||||
|
string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (record.Length != 4)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||||
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||||
|
if (collection == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
|
|
||||||
|
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
foreach (string elem in set)
|
||||||
|
{
|
||||||
|
if (elem?.CreateDrawningAirplane() is T airplane)
|
||||||
|
{
|
||||||
|
if (collection.Insert(airplane) == -1)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_storages.Add(record[0], collection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user