0.4.16 Reading Information From a Text File

Reading Brings Persistent Information Back Into the Program

Writing moves information from the running application into a file.

Reading moves information in the opposite direction.

Suppose lineup.txt already contains:

Plain text
Jordan starts at forward.

A simple C# API for reading all text from the file is:

C#
System.IO.File.ReadAllText

A Simple Read

An instructional example is:

C#
string lineupNote = System.IO.File.ReadAllText("lineup.txt");

Read the statement as:

Read all of the text from lineup.txt and store that text in the string variable lineupNote.

The file existed before this statement.

After the statement runs, the text is also available in the current program.

The File and Variable Are Separate

Suppose the file contains:

Plain text
Jordan starts at forward.

After:

C#
string lineupNote = System.IO.File.ReadAllText("lineup.txt");

the variable now contains the same text.

There are still two distinct locations:

The read operation copied the information into the running program.

The Program Can Use the Value After Reading

Once the text is in memory, the program can work with it like other string data.

For example:

C#
string lineupNote = System.IO.File.ReadAllText("lineup.txt");

System.Console.WriteLine(lineupNote);

The first statement reads the persistent information.

The second displays the runtime value.

The file itself is not being displayed directly.

The program first reads the contents into memory.

Reading Does Not Remove the File

ReadAllText reads the file's contents.

It does not normally delete the file.

The persistent copy remains available unless another operation later changes or removes it.

This means the same saved information can be read during more than one program run.

Read After a Previous Write

A simple persistence story is:

First run

C#
string lineupNote = "Jordan starts at forward.";

System.IO.File.WriteAllText("lineup.txt", lineupNote);

The program writes the text.

Then the application closes.

Later run

C#
string lineupNote = System.IO.File.ReadAllText("lineup.txt");

The program reads the previously saved text.

The information survived between the two runs because it was stored in the file.

Use the Course-Provided File Path

A real course project may specify where the file is located.

Use that location.

If you change the path without understanding the project, the program may read from a different file or fail to find the intended one.

File location is part of the storage context.

Missing Files Are a Different Problem

If the code attempts to read a file that does not exist, the operation cannot return the expected text.

Robust software should handle that situation appropriately.

Detailed exception handling comes later.

In this introductory module, use the supplied project and storage sequence so you can focus on the core read operation.

Reading Is Not the Same as Parsing

ReadAllText returns text.

If the file contains:

Plain text
Jordan,7,true

the result is still one string containing those characters.

Turning that text into separate fields or records is a different task.

Structured data and CSV files appear later in FIT.

For now, the important movement is:

Plain text
text file
    ↓
File.ReadAllText(...)
    ↓
string in memory

Write and Read Form a Persistence Cycle

The two basic operations now connect.

Save

Plain text
memory → WriteAllText → file

Load

Plain text
file → ReadAllText → memory

The C# methods are small.

The conceptual shift is larger.

You can now distinguish information that exists only during the current execution from information that has been persisted and recovered later.