Использовал StreamWriter и StreamReader

This commit is contained in:
insideq 2024-04-25 19:16:55 +04:00
parent e61d3cb96a
commit 20d212bc57
4 changed files with 69 additions and 78 deletions

View File

@ -112,24 +112,24 @@ public class StorageCollection<T>
File.Delete(filename); File.Delete(filename);
} }
StringBuilder sb = new(); using (StreamWriter writer = new StreamWriter(filename))
{
sb.Append(_collectionKey); writer.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages) foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
sb.Append(Environment.NewLine); writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции // не сохраняем пустые коллекции
if (value.Value.Count == 0) if (value.Value.Count == 0)
{ {
continue; continue;
} }
sb.Append(value.Key); writer.Write(value.Key);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType); writer.Write(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
sb.Append(value.Value.MaxCount); writer.Write(value.Value.MaxCount);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems()) foreach (T? item in value.Value.GetItems())
{ {
@ -139,14 +139,11 @@ public class StorageCollection<T>
continue; continue;
} }
sb.Append(data); writer.Write(data);
sb.Append(_separatorItems); writer.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;
} }
@ -162,33 +159,26 @@ public class StorageCollection<T>
return false; return false;
} }
string bufferTextFromFile = ""; using (StreamReader reader = File.OpenText(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
byte[] b = new byte[fs.Length]; string str = reader.ReadLine();
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); if (str == null || str.Length == 0)
if (strs == null || strs.Length == 0)
{ {
return false; return false;
} }
if (!strs[0].Equals(_collectionKey)) if (!str.StartsWith(_collectionKey))
{ {
//если нет такой записи, то это не те данные //если нет такой записи, то это не те данные
return false; return false;
} }
_storages.Clear(); _storages.Clear();
foreach (string data in strs) string strs = "";
while ((strs = reader.ReadLine()) != null)
{ {
string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4) if (record.Length != 4)
{ {
continue; continue;
@ -217,7 +207,7 @@ public class StorageCollection<T>
_storages.Add(record[0], collection); _storages.Add(record[0], collection);
} }
}
return true; return true;
} }

View File

@ -1,4 +1,5 @@
using ProjectExcavator.Entities; using ProjectExcavator.Entities;
using System.Security.Policy;
namespace ProjectExcavator.Drawnings; namespace ProjectExcavator.Drawnings;
@ -99,9 +100,9 @@ public class DrawningBulldozer
/// Конструктор, принимающий объект EntityBulldozer в качестве параметра /// Конструктор, принимающий объект EntityBulldozer в качестве параметра
/// </summary> /// </summary>
/// <param name="entityBulldozer"></param> /// <param name="entityBulldozer"></param>
public DrawningBulldozer(EntityBulldozer entityBulldozer) public DrawningBulldozer(EntityBulldozer bull) : this()
{ {
EntityBulldozer = entityBulldozer; EntityBulldozer = new EntityBulldozer(bull.Speed, bull.Weight, bull.BodyColor);
} }
/// <summary> /// <summary>

View File

@ -25,9 +25,9 @@ public class DrawningExcavator : DrawningBulldozer
/// Конструктор, принимающий объект EntityBulldozer в качестве параметра /// Конструктор, принимающий объект EntityBulldozer в качестве параметра
/// </summary> /// </summary>
/// <param name="entityBulldozer"></param> /// <param name="entityBulldozer"></param>
public DrawningExcavator(EntityBulldozer entityBulldozer) public DrawningExcavator(EntityExcavator exc) : base(150, 80)
{ {
EntityBulldozer = entityBulldozer; EntityBulldozer = new EntityExcavator(exc.Speed, exc.Weight, exc.BodyColor, exc.AdditionalColor, exc.Prop, exc.Ladle);
} }
public override void DrawTransport(Graphics g) public override void DrawTransport(Graphics g)

View File

@ -23,7 +23,7 @@ public static class ExtentionDrawningBulldozer
EntityBulldozer? bulldozer = EntityExcavator.CreateEntityExcavator(strs); EntityBulldozer? bulldozer = EntityExcavator.CreateEntityExcavator(strs);
if (bulldozer != null) if (bulldozer != null)
{ {
return new DrawningExcavator(bulldozer); return new DrawningExcavator((EntityExcavator)bulldozer);
} }
bulldozer = EntityBulldozer.CreateEntityBulldozer(strs); bulldozer = EntityBulldozer.CreateEntityBulldozer(strs);