/// <summary> /// Saving to a text file what is in a character list /// </summary> private void SaveCharacters() { StreamWriter sw = null; try { sw = new StreamWriter(FILENAME); foreach (Character character in characters) { sw.WriteLine(CharacterMapper.toStringCSV(character)); sw.Flush(); } } catch (Exception e) { Console.WriteLine("Something went wrong"); } finally { if (sw != null) { sw.Close(); } } }
/// <summary> /// Load from a text file into the character list /// </summary> private void LoadCharacters() { List <Character> results = new List <Character>(); StreamReader sr = null; try { sr = new StreamReader(FILENAME); string row = ""; while ((row = sr.ReadLine()) != null) { Character c = CharacterMapper.ToCharacter(row); results.Add(c); } characters = results; } catch (FileNotFoundException fileNotFound) { Console.WriteLine(fileNotFound.FileName + " was not found"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (sr != null) { sr.Close(); } } }