| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.IO; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides synchronous helper methods for reading and deserializing file contents. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// All methods validate the provided path and throw <see cref="ArgumentException"/> for null/whitespace and <see cref=" |
| | | 10 | | /// </remarks> |
| | | 11 | | public static class FileReader |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Reads the entire contents of a text file. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="path">Absolute or relative path to the file.</param> |
| | | 17 | | /// <returns>File content as a string.</returns> |
| | | 18 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 19 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 20 | | public static string Read(string path) |
| | 5 | 21 | | { |
| | 5 | 22 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 23 | | { |
| | 3 | 24 | | throw new ArgumentException("Path cannot be null or whitespace", nameof(path)); |
| | | 25 | | } |
| | | 26 | | |
| | 2 | 27 | | return File.Exists(path) |
| | 2 | 28 | | ? File.ReadAllText(path) |
| | 2 | 29 | | : throw new FileNotFoundException($"The file at path '{path}' does not exist", path); |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Reads a delimited text file (e.g. CSV) mapping each header to its column values. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="path">Path to the file.</param> |
| | | 36 | | /// <param name="separator">Delimiter separating fields (e.g. <c>','</c>).</param> |
| | | 37 | | /// <returns>A dictionary keyed by column header mapping to arrays of its values.</returns> |
| | | 38 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 39 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 40 | | /// <exception cref="InvalidOperationException">File must contain a header and at least one data line.</exception> |
| | | 41 | | public static Dictionary<string, string[]> ReadAsDictionary(string path, char separator) |
| | 8 | 42 | | { |
| | 8 | 43 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 44 | | { |
| | 3 | 45 | | throw new ArgumentException("Path cannot be null or whitespace", nameof(path)); |
| | | 46 | | } |
| | | 47 | | |
| | 5 | 48 | | if (!File.Exists(path)) |
| | 1 | 49 | | { |
| | 1 | 50 | | throw new FileNotFoundException($"The file at path '{path}' does not exist", path); |
| | | 51 | | } |
| | | 52 | | |
| | 4 | 53 | | var lines = File.ReadAllLines(path); |
| | | 54 | | |
| | 4 | 55 | | if (lines.Length < 2) |
| | 1 | 56 | | { |
| | 1 | 57 | | throw new InvalidOperationException("File must have at least a header and one data line"); |
| | | 58 | | } |
| | | 59 | | |
| | 3 | 60 | | var headers = lines[0].Split(separator); |
| | 3 | 61 | | var columnLists = new List<string>[headers.Length]; |
| | 22 | 62 | | for (int i = 0; i < headers.Length; i++) |
| | 8 | 63 | | { |
| | 8 | 64 | | columnLists[i] = new List<string>(); |
| | 8 | 65 | | } |
| | | 66 | | |
| | 18 | 67 | | for (var row = 1; row < lines.Length; row++) |
| | 6 | 68 | | { |
| | 6 | 69 | | var values = lines[row].Split(separator); |
| | | 70 | | |
| | 44 | 71 | | for (var col = 0; col < headers.Length; col++) |
| | 16 | 72 | | { |
| | 16 | 73 | | var value = col < values.Length ? values[col] : string.Empty; |
| | 16 | 74 | | columnLists[col].Add(value); |
| | 16 | 75 | | } |
| | 6 | 76 | | } |
| | | 77 | | |
| | 3 | 78 | | var dict = new Dictionary<string, string[]>(headers.Length); |
| | 22 | 79 | | for (int i = 0; i < headers.Length; i++) |
| | 8 | 80 | | { |
| | 8 | 81 | | dict[headers[i]] = columnLists[i].ToArray(); |
| | 8 | 82 | | } |
| | | 83 | | |
| | 3 | 84 | | return dict; |
| | 3 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Reads all lines of a text file into an array. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="path">Path to the file.</param> |
| | | 91 | | /// <returns>Array of lines.</returns> |
| | | 92 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 93 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 94 | | public static string[] ReadLines(string path) |
| | 5 | 95 | | { |
| | 5 | 96 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 97 | | { |
| | 3 | 98 | | throw new ArgumentException("Path cannot be null or whitespace", nameof(path)); |
| | | 99 | | } |
| | | 100 | | |
| | 2 | 101 | | return File.Exists(path) |
| | 2 | 102 | | ? File.ReadAllLines(path) |
| | 2 | 103 | | : throw new FileNotFoundException($"The file at path '{path}' does not exist", path); |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Reads a JSON file and deserializes its content into the specified type. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <typeparam name="T">Target type.</typeparam> |
| | | 110 | | /// <param name="path">Path to the JSON file.</param> |
| | | 111 | | /// <returns>Deserialized object or <c>null</c> if content is empty or invalid JSON.</returns> |
| | | 112 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 113 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 114 | | public static T? ReadAndDeserialize<T>(string path) |
| | 6 | 115 | | { |
| | 6 | 116 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 117 | | { |
| | 3 | 118 | | throw new ArgumentException("Path cannot be null or whitespace", nameof(path)); |
| | | 119 | | } |
| | | 120 | | |
| | 3 | 121 | | if (!File.Exists(path)) |
| | 1 | 122 | | { |
| | 1 | 123 | | throw new FileNotFoundException($"The file at path '{path}' does not exist", path); |
| | | 124 | | } |
| | | 125 | | |
| | 2 | 126 | | var content = File.ReadAllText(path); |
| | | 127 | | |
| | 2 | 128 | | return JsonSerializer.Deserialize<T>(content); |
| | 1 | 129 | | } |
| | | 130 | | } |