-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReading.cs
55 lines (45 loc) · 1.36 KB
/
FileReading.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
namespace ConsoleInteractingFilesApp;
public class FileReading
{
#region Class properties, constructor initialization.
/// <summary>
/// The path to the file.
/// </summary>
private readonly string _fileName;
/// <summary>
/// Constructor for reading the file.
/// </summary>
/// <param name="fileName"> File name. </param>
public FileReading(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentNullException(nameof(fileName), "The file path cannot be empty!");
}
if (!File.Exists(fileName))
{
throw new FileNotFoundException("The specified file could not be found!", nameof(fileName));
}
_fileName = fileName;
}
#endregion
#region Methods of reading information from a file.
/// <summary>
/// Reading data in a text representation.
/// </summary>
/// <returns> File data in a text representation. </returns>
public string ReadText()
{
using var reader = new StreamReader(_fileName);
return reader.ReadToEnd();
}
/// <summary>
/// Reading data in byte representation.
/// </summary>
/// <returns> File data in byte array representation. </returns>
public IEnumerable<byte> ReadBytes()
{
return File.ReadAllBytes(_fileName);
}
#endregion
}