Last active
July 29, 2016 11:49
-
-
Save zharro/cd0a65182c50b531856a17d30d2da333 to your computer and use it in GitHub Desktop.
Interacting with file system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Agent for interacting with file system | |
/// </summary> | |
public class FileSystemAgent : IFileSystemAgent | |
{ | |
public IList<string> ReadAllLines(string fileName) | |
{ | |
if (!File.Exists(fileName)) | |
{ | |
throw new ArgumentException($"File {fileName} doesn't exist"); | |
} | |
return File.ReadAllLines(fileName).ToList(); | |
} | |
public IEnumerable<string> ReadLineByLine(string fileName) | |
{ | |
if (!File.Exists(fileName)) | |
{ | |
throw new ArgumentException($"File {fileName} doesn't exist"); | |
} | |
using (var reader = File.OpenText(fileName)) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
yield return line; | |
} | |
} | |
} | |
public void WriteAllLines(IEnumerable<string> lines, string fileName) | |
{ | |
using (StreamWriter file = new StreamWriter(fileName)) | |
{ | |
foreach (string line in lines) | |
{ | |
file.WriteLine(line); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment