1. Home
  2. The Cynical Philosopher

Jul 13, 2023

How C# reads, writes, and manages files

28 min → 2:10

Overview

The host of The Cynical Philosopher explains how to create, read, write, and manage files in C# using the File, FileStream, StreamReader, and StreamWriter classes alongside relative and absolute directories.

3 things to know

The Cynical Philosopher: File class handles creating and deleting files

The Cynical Philosopher explains that the static File class in System.IO provides built-in tools for common tasks like creating, copying, deleting, reading, and writing to files.

▶ Watch at 1:27

The Cynical Philosopher says FileStream controls file access and sharing

FileStream works with data as a sequence of bytes and offers more control over file access and sharing than the basic File class, says The Cynical Philosopher.

More

A lower-level class that provides a stream for reading from and writing to files, working with data as a sequence of bytes.

▶ Watch at 26:57

StreamReader and StreamWriter handle UTF-8, says The Cynical Philosopher

The Cynical Philosopher explains that StreamReader and StreamWriter sit on top of streams to handle character encoding like UTF-8 and provide convenient methods like ReadLine() and WriteLine().

More

These classes are built on top of streams to make reading and writing text easier.

Also in this episode

Closing files frees system resources, says The Cynical Philosopher

The Cynical Philosopher states that the standard procedure for working with a file is to open it, read or write to it, and close it to release system resources.

▶ Watch at 0:31

Relative directories make code portable, says The Cynical Philosopher

The Cynical Philosopher notes that a relative directory points to a file starting from the current project's execution folder, typically bin\Debug, which makes the code more portable.

The Cynical Philosopher says absolute directories start from root drives

An absolute directory gives the full, explicit path to a file starting from the root drive, such as C:\Documents\Text.txt, according to The Cynical Philosopher.

The Cynical Philosopher: File paths can be specified two ways

The Cynical Philosopher explains that a file directory path can be specified in two ways.

Quotes

“the tl;dr is that StreamReader and StreamWriter have more functionality (methods) than FileStream... [and] handle text better than FileStream, i.e., text encoding.”

▶ Watch at 26:57

“the relative directory... is just going to save and uh look for the file in the project folder under bin\Debug.”

▶ Watch at 1:27

“when we do this we want to open the file we edit the file or read the file or whatever the case may be and then close the file.”

▶ Watch at 0:31

Full summary

Episode Overview

  • An introduction to the C# classes used for handling file operations, including the File class, FileStream, StreamReader, and StreamWriter.
  • A breakdown of the difference between absolute and relative file directories for locating and saving files.
  • An overview of common methods available in the File class for creating, reading, writing, copying, and deleting files.
  • A walkthrough of C# code examples demonstrating how to create, read from, write to, and append text to files.

Key Concepts

  • File Class: A static class in System.IO that provides methods for common file operations like creating, copying, deleting, reading, and writing to files.
  • File Directory: The path to a file can be specified in two ways:
    • Absolute Directory: The full, explicit path from the root drive (e.g., C:\Documents\Text.txt).
    • Relative Directory: The path relative to the current project's execution folder (typically bin\Debug), making the code more portable.
  • File Workflow: The standard procedure for interacting with a file is to open it, perform a read or write operation, and then close it to release the system resources.
  • FileStream: A lower-level class that provides a stream for reading from and writing to files, working with data as a sequence of bytes. It offers more control over file access and sharing than the File class.
  • StreamReader & StreamWriter: These classes are built on top of streams to make reading and writing text easier. They handle character encoding (e.g., UTF-8) and provide convenient methods like ReadLine() and WriteLine().

Quotes

  • At 00:31 - "when we do this we want to open the file we edit the file or read the file or whatever the case may be and then close the file." - Explaining the fundamental workflow of file operations.
  • At 01:27 - "the relative directory... is just going to save and uh look for the file in the project folder under bin\Debug." - Clarifying where files are located by default when using relative paths in a C# project.
  • At 26:57 - "the tl;dr is that StreamReader and StreamWriter have more functionality (methods) than FileStream... [and] handle text better than FileStream, i.e., text encoding." - Summarizing the key reasons to use StreamReader and StreamWriter over the more basic FileStream when working with text.

Takeaways

  • Use the File class for simple, straightforward file I/O tasks, such as reading or writing the entire contents of a small file at once.
  • Understand the difference between absolute and relative paths. Using relative paths makes your application more portable as it doesn't rely on a specific hardcoded file structure.
  • When you need more control, such as handling large files line-by-line or managing specific character encodings, use FileStream in conjunction with StreamReader (for reading) and StreamWriter (for writing).
  • Always check if a file exists before trying to read from it to prevent a File not found exception, and ensure you close file streams and readers/writers to release resources.

Watch the full episode 28 min

Read along with the audio summary

Reading from a file before checking if it exists can trigger a File not found exception. This episode of the show, which came out on July 13, 2023, walks through the C sharp classes used for handling file operations. By the end, you will know how to locate, create, read, write, copy, and delete files using the File class, FileStream, StreamReader, and StreamWriter.

The File class handles everyday file operations in one place.

Writing code to manage files does not always require complex setups. Inside System dot I O, which is the part of C sharp used for input and output, there is a static class called the File class. It provides built in methods for common operations like creating, copying, deleting, reading, and writing to files, as well as appending text. For simple, straightforward tasks, such as reading or writing the entire contents of a small file at once, use the File class.

Once you know which class to use, there are two ways to specify the file directory path so your code can locate and save files.

An absolute directory points to the exact same spot every time.

Hardcoding a file location means leaving nothing to guesswork. An absolute directory is the full, explicit path starting from the root drive, such as C colon backslash Documents backslash Text dot txt. It tells the program the exact structure to follow from the very beginning of the drive.

In contrast, a relative directory makes your code more portable.

Relying on a specific hardcoded file structure can limit where your application works. A relative directory specifies the path relative to the current project's execution folder. In a C sharp project, this is typically the bin backslash Debug folder, which is where the program saves and looks for the file by default. Use relative paths when you want to make your application more portable.

The standard file workflow protects your system resources.

Opening a file is only part of the job. The standard procedure for interacting with a file is to open it, perform your read, write, or edit operation, and then close it. Closing the file is necessary to release the system resources it was using. Always check if a file exists before trying to read from it to prevent a File not found exception, and ensure you close your files when you are done.

FileStream gives you deeper control by working with bytes.

Sometimes the basic File class is not enough for the job. FileStream is a lower level class that provides a stream for reading from and writing to files by working with data as a sequence of bytes. Because it works at this lower level, it offers more control over file access and sharing than the File class.

Building on top of streams, StreamReader and StreamWriter make handling text much easier.

Working with raw bytes is not ideal when you just want to manage words. StreamReader and StreamWriter are built on top of streams to handle text better than a basic FileStream. They have more functionality, manage character encoding like U T F 8, and provide convenient methods like ReadLine and WriteLine. When you need more control, such as handling large files line by line or managing specific character encodings, use FileStream together with StreamReader for reading and StreamWriter for writing, and always close your streams, readers, and writers to release resources.

Matching the right C sharp tool and directory path to your task keeps your file operations clean and portable. Always check that a file exists before reading it, and close your streams, readers, and writers to release system resources.

More from The Cynical Philosopher

Related Episodes