diff --git a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs index 2c0555f..b0d3ff6 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs @@ -116,41 +116,40 @@ public class StorageCollection File.Delete(filename); } - StringBuilder sb = new(); - - sb.Append(_collectionKey); - foreach (KeyValuePair> value in _storages) + using (StreamWriter wr = new StreamWriter(filename)) { - sb.Append(Environment.NewLine); - // Не сохраняем пустые коллекции - if (value.Value.Count == 0) + wr.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - continue; - } - - sb.Append(value.Key); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.MaxCount); - sb.Append(_separatorForKeyValue); - - foreach (T? item in value.Value.GetItems()) - { - string data = item?.GetDataForSave() ?? string.Empty; - if (string.IsNullOrEmpty(data)) + StringBuilder sb = new StringBuilder(); + sb.Append(Environment.NewLine); + // Не сохраняем пустые коллекции + if (value.Value.Count == 0) { continue; } - sb.Append(data); - sb.Append(_separatorItems); + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + + sb.Append(data); + sb.Append(_separatorItems); + } + wr.Write(sb); } } - - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); - fs.Write(info, 0, info.Length); } /// @@ -164,61 +163,54 @@ public class StorageCollection throw new Exception("Файл не существует"); } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new StreamReader(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = sr.ReadLine(); + if (str == null || str.Length == 0) { - bufferTextFromFile += temp.GetString(b); - } - } - - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - throw new Exception("В файле нет данных"); - } - - if (!strs[0].Equals(_collectionKey)) - { - throw new Exception("В файле неверные данные"); - } - - _storages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) - { - continue; + throw new Exception("В файле нет данных"); } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType) ?? - throw new Exception("Не удалось определить тип коллекции:" + record[1]); - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + if (!str.Equals(_collectionKey)) { - if (elem?.CreateDrawingBoat() is T boat) + throw new Exception("В файле неверные данные"); + } + + _storages.Clear(); + string data = ""; + while ((data = sr.ReadLine()) != null) + { + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { - try + continue; + } + + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType) ?? + throw new Exception("Не удалось определить тип коллекции:" + record[1]); + collection.MaxCount = Convert.ToInt32(record[2]); + + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + if (elem?.CreateDrawingBoat() is T boat) { - if (collection.Insert(boat) == -1) + try { - throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); + if (collection.Insert(boat) == -1) + { + throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); + } + } + catch (CollectionOverflowException ex) + { + throw new Exception("Коллекция переполнена", ex); } } - catch (CollectionOverflowException ex) - { - throw new Exception("Коллекция переполнена", ex); - } + _storages.Add(record[0], collection); } } - _storages.Add(record[0], collection); } }