From 15565fd27be540908aed293a2a19d46dd23302dd Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Wed, 17 Apr 2024 17:05:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D1=8C=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20StreamWriter=20=D0=B8?= =?UTF-8?q?=20StreamReader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 114 ++++++++---------- 1 file changed, 52 insertions(+), 62 deletions(-) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs index 2935ca3..81db8f9 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.IO; +using System.Text; using ProjectAirplaneWithRadar.Drawnings; namespace ProjectAirplaneWithRadar.CollectionGenericObjects @@ -103,24 +104,25 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects return false; if(File.Exists(filename)) - File.Delete(filename); + File.Delete(filename); - StringBuilder sb = new(); - - sb.Append(_collectionKey); - foreach(KeyValuePair> value in _storages) + using FileStream fs = new(filename, FileMode.Create); + using StreamWriter sw = new StreamWriter(fs); + sw.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - sb.Append(Environment.NewLine); - + sw.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); + sw.Write(value.Key); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.GetCollectionType); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.MaxCount); + sw.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) { @@ -130,14 +132,10 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects continue; } - sb.Append(data); - sb.Append(_separatorItems); + sw.Write(data); + 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; } @@ -153,59 +151,51 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects return false; } - string bufferTextFromFile = ""; using (FileStream fs = new(filename, FileMode.Open)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) - { - bufferTextFromFile += temp.GetString(b); - } - } + using StreamReader sr = new StreamReader(fs); - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.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? collection = StorageCollection.CreateCollection(collectionType); - if (collection == null) + string str = sr.ReadLine(); + if (str == null || str.Length == 0) { return false; } - 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?.CreateDrawningAirplane() is T airplane) - { - if (collection.Insert(airplane) == -1) - return false; - } + 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? 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?.CreateDrawningAirplane() is T airplane) + { + if (collection.Insert(airplane) == -1) + return false; + } + } + _storages.Add(record[0], collection); + } } - return true; }