diff --git a/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/StorageCollection.cs b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/StorageCollection.cs index 60d3d8a..831909c 100644 --- a/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectAirBomber/ProjectAirBomber/CollectionGenericObjects/StorageCollection.cs @@ -96,7 +96,7 @@ public class StorageCollection } /// - /// Сохранение информации по автомобилям в хранилище в файл + /// Сохранение информации по бомбардировщикам в хранилище в файл /// /// Путь и имя файла /// true - сохранение прошло успешно, false - ошибка при сохранении данных @@ -112,24 +112,23 @@ public class StorageCollection File.Delete(filename); } - StringBuilder sb = new(); - - sb.Append(_collectionKey); + using StreamWriter writer = new StreamWriter(filename); + writer.Write(_collectionKey); foreach (KeyValuePair> value in _storages) { - sb.Append(Environment.NewLine); + writer.Write(Environment.NewLine); // не сохраняем пустые коллекции if (value.Value.Count == 0) { continue; } - sb.Append(value.Key); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.MaxCount); - sb.Append(_separatorForKeyValue); + writer.Write(value.Key); + writer.Write(_separatorForKeyValue); + writer.Write(value.Value.GetCollectionType); + writer.Write(_separatorForKeyValue); + writer.Write(value.Value.MaxCount); + writer.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) { @@ -139,14 +138,11 @@ public class StorageCollection continue; } - sb.Append(data); - sb.Append(_separatorItems); + writer.Write(data); + 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; } @@ -162,53 +158,51 @@ public class StorageCollection return false; } - using (StreamReader reader = File.OpenText(filename)) + using StreamReader reader = File.OpenText(filename); + string? str = reader.ReadLine(); + if (str == null || str.Length == 0) { - string? str = reader.ReadLine(); - if (str == null || str.Length == 0) + return false; + } + if (!str.StartsWith(_collectionKey)) + { + return false; + } + _storages.Clear(); + string? strs = ""; + + while ((strs = reader.ReadLine()) != null) + { + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) + { + continue; + } + + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + + if (collection == null) { return false; } - if (!str.StartsWith(_collectionKey)) + collection.MaxCount = Convert.ToInt32(record[2]); + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + + foreach (string elem in set) { - return false; - } - _storages.Clear(); - string? strs = ""; - - while ((strs = reader.ReadLine()) != null) - { - string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) + if (elem?.CreateDrawningBomber() is T bomber) { - continue; - } - - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.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?.CreateDrawningBomber() is T bomber) + if (collection.Insert(bomber) == -1) { - if (collection.Insert(bomber) == -1) - { - return false; - } + return false; } } - _storages.Add(record[0], collection); } - - return true; + _storages.Add(record[0], collection); } + + return true; } ///