English
Summary
Conclusions
The System.IO namespace contains file system classes (File, Directory, Path, FileInfo, DirectoryInfo), byte streams (Stream, FileStream), and classes for reading and writing on top of streams (StreamReader, StreamWriter, BinaryReader, BinaryWriter). Paths are built with Path methods and are resolved relative to the working directory or the application directory. Text files use UTF-8 by default; other encodings are specified explicitly. Streams implement IDisposable, so they are created in using; otherwise, data may not be written and the file remains locked. Binary files with fixed-length records allow random access with the Seek method. File operations are accompanied by I/O exception handling, and CSV is parsed using the invariant culture. System.Text.Json serializes objects to JSON and back; JsonSerializerOptions and attributes control names, formatting, enumerations, encoding, and polymorphic types.
Self-check questions
- How does an absolute path differ from a relative one? What is the working directory?
- What is
Path.Combineused for? - How does
File.ReadAllLinesdiffer fromFile.ReadLines? - What are
FileInfoandDirectoryInfofor? - What is text encoding? Which encoding is used by default?
- What is a stream? Which stream classes do you know?
- What
FileModemodes andFileAccessaccess types exist? - Why must streams be closed? How do you do this reliably?
- How do you get random access to a record in a binary file?
- What exceptions can occur when working with files?
- What difficulties arise when processing CSV?
- What are serialization and deserialization?
- What settings does
JsonSerializerOptionsprovide? - What are the
[JsonPropertyName],[JsonIgnore], and[JsonRequired]attributes used for? - How do you serialize a collection of objects of derived types?
Useful links
- File and stream I/O: https://learn.microsoft.com/dotnet/standard/io/
- Common I/O tasks: https://learn.microsoft.com/dotnet/standard/io/common-i-o-tasks
- The
Fileclass: https://learn.microsoft.com/dotnet/api/system.io.file System.Text.Json: https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/overview- JSON serialization and deserialization: https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/how-to
- Polymorphic serialization: https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/polymorphism