IO
Synchronous and asynchronous file readers for text, lines, delimited data and JSON.
Features
FileReader(synchronous): read a file as a string, as an array of lines, as a separator-delimited dictionary, or deserialize its JSON content into a typed object. Deserialization throwsJsonExceptionfor an empty, malformed or mismatched payload, and returnsnullonly for the JSON literalnull.FileReaderAsync(asynchronous): the same four operations asFileReader, returningTask-wrapped results and accepting aCancellationToken. Behavior is identical to the synchronous reader.- The delimited reader is a plain split on the separator, not an RFC 4180 parser: quoting is not interpreted, so a quoted field containing the separator or a line break is split like any other. It throws
InvalidOperationExceptionon a duplicate header name rather than silently dropping the column.
Class Diagram
classDiagram
namespace IO {
class FileReader {
<<static>>
+string Read(string path)
+string[] ReadLines(string path)
+Dictionary~string, string[]~ ReadAsDictionary(string path, char separator)
+T ReadAndDeserialize~T~(string path)
}
class FileReaderAsync {
<<static>>
+Task~string~ ReadAsync(string path)
+Task~string[]~ ReadLinesAsync(string path)
+Task~Dictionary~string, string[]~~ ReadAsDictionaryAsync(string path, char separator)
+Task~T~ ReadAndDeserializeAsync~T~(string path, CancellationToken)
}
}Usage
Synchronous
using ArturRios.Util.IO;
// Entire file as a single string
string content = FileReader.Read("/data/notes.txt");
// Array of lines
string[] lines = FileReader.ReadLines("/data/notes.txt");
// Separator-delimited file → dictionary
// Key = first column value, Value = remaining columns as string[]
Dictionary<string, string[]> table = FileReader.ReadAsDictionary("/data/config.csv", ',');
// JSON file → typed object (uses System.Text.Json)
AppConfig config = FileReader.ReadAndDeserialize<AppConfig>("/data/config.json");
Asynchronous
using ArturRios.Util.IO;
string content = await FileReaderAsync.ReadAsync("/data/notes.txt");
string[] lines = await FileReaderAsync.ReadLinesAsync("/data/notes.txt");
Dictionary<string, string[]> table =
await FileReaderAsync.ReadAsDictionaryAsync("/data/config.csv", ',');
AppConfig config =
await FileReaderAsync.ReadAndDeserializeAsync<AppConfig>("/data/config.json");