< Summary

Information
Class: ArturRios.Util.IO.FileReader
Assembly: ArturRios.Util
File(s): D:\Repositories\dotnet-util\src\IO\FileReader.cs
Line coverage
100%
Covered lines: 59
Uncovered lines: 0
Coverable lines: 59
Total lines: 130
Line coverage: 100%
Branch coverage
100%
Covered branches: 28
Total branches: 28
Branch coverage: 100%
Method coverage
100%
Covered methods: 4
Fully covered methods: 0
Total methods: 4
Method coverage: 100%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)100%44100%
ReadAsDictionary(...)100%1616100%
ReadLines(...)100%44100%
ReadAndDeserialize(...)100%44100%

File(s)

D:\Repositories\dotnet-util\src\IO\FileReader.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace 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>
 11public 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)
 521    {
 522        if (string.IsNullOrWhiteSpace(path))
 323        {
 324            throw new ArgumentException("Path cannot be null or whitespace", nameof(path));
 25        }
 26
 227        return File.Exists(path)
 228            ? File.ReadAllText(path)
 229            : throw new FileNotFoundException($"The file at path '{path}' does not exist", path);
 130    }
 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)
 842    {
 843        if (string.IsNullOrWhiteSpace(path))
 344        {
 345            throw new ArgumentException("Path cannot be null or whitespace", nameof(path));
 46        }
 47
 548        if (!File.Exists(path))
 149        {
 150            throw new FileNotFoundException($"The file at path '{path}' does not exist", path);
 51        }
 52
 453        var lines = File.ReadAllLines(path);
 54
 455        if (lines.Length < 2)
 156        {
 157            throw new InvalidOperationException("File must have at least a header and one data line");
 58        }
 59
 360        var headers = lines[0].Split(separator);
 361        var columnLists = new List<string>[headers.Length];
 2262        for (int i = 0; i < headers.Length; i++)
 863        {
 864            columnLists[i] = new List<string>();
 865        }
 66
 1867        for (var row = 1; row < lines.Length; row++)
 668        {
 669            var values = lines[row].Split(separator);
 70
 4471            for (var col = 0; col < headers.Length; col++)
 1672            {
 1673                var value = col < values.Length ? values[col] : string.Empty;
 1674                columnLists[col].Add(value);
 1675            }
 676        }
 77
 378        var dict = new Dictionary<string, string[]>(headers.Length);
 2279        for (int i = 0; i < headers.Length; i++)
 880        {
 881            dict[headers[i]] = columnLists[i].ToArray();
 882        }
 83
 384        return dict;
 385    }
 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)
 595    {
 596        if (string.IsNullOrWhiteSpace(path))
 397        {
 398            throw new ArgumentException("Path cannot be null or whitespace", nameof(path));
 99        }
 100
 2101        return File.Exists(path)
 2102            ? File.ReadAllLines(path)
 2103            : throw new FileNotFoundException($"The file at path '{path}' does not exist", path);
 1104    }
 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)
 6115    {
 6116        if (string.IsNullOrWhiteSpace(path))
 3117        {
 3118            throw new ArgumentException("Path cannot be null or whitespace", nameof(path));
 119        }
 120
 3121        if (!File.Exists(path))
 1122        {
 1123            throw new FileNotFoundException($"The file at path '{path}' does not exist", path);
 124        }
 125
 2126        var content = File.ReadAllText(path);
 127
 2128        return JsonSerializer.Deserialize<T>(content);
 1129    }
 130}