0
0
C Sharp (C#)programming~10 mins

StreamReader and StreamWriter in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - StreamReader and StreamWriter
Start
Open file with StreamWriter
Write text to file
Close StreamWriter
Open file with StreamReader
Read text from file
Close StreamReader
End
This flow shows writing text to a file using StreamWriter, then reading it back using StreamReader.
Execution Sample
C Sharp (C#)
using System.IO;

var writer = new StreamWriter("test.txt");
writer.WriteLine("Hello World");
writer.Close();

var reader = new StreamReader("test.txt");
var text = reader.ReadLine();
reader.Close();
Writes 'Hello World' to a file and then reads it back.
Execution Table
StepActionFile StateVariable ValuesOutput
1Open StreamWriter for 'test.txt'File opened for writing (empty)writer=open, reader=null, text=null
2WriteLine 'Hello World'File contains 'Hello World\n'writer=open, reader=null, text=null
3Close StreamWriterFile saved with 'Hello World\n'writer=closed, reader=null, text=null
4Open StreamReader for 'test.txt'File opened for readingwriter=closed, reader=open, text=null
5ReadLine from fileFile unchangedwriter=closed, reader=open, text='Hello World'
6Close StreamReaderFile closedwriter=closed, reader=closed, text='Hello World'
💡 StreamReader and StreamWriter closed, text read successfully.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
writernullopenclosedclosed
readernullnullopenclosed
textnullnull'Hello World''Hello World'
Key Moments - 2 Insights
Why do we need to close StreamWriter before opening StreamReader?
Closing StreamWriter flushes and saves the data to the file. Without closing, StreamReader might read incomplete or no data. See execution_table steps 3 and 4.
What happens if we try to read from StreamReader before writing anything?
The file would be empty or not created, so StreamReader.ReadLine() returns null or empty. In our trace, writing happens before reading (steps 2 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'text' after step 5?
A'Hello World'
Bnull
CEmpty string
DThrows error
💡 Hint
Check the 'Variable Values' column at step 5 in execution_table.
At which step is the file content actually saved to disk?
AStep 4 - Open StreamReader
BStep 2 - WriteLine
CStep 3 - Close StreamWriter
DStep 6 - Close StreamReader
💡 Hint
Look at the 'File State' column; file is saved when StreamWriter is closed.
If we skip closing StreamWriter, what likely happens when opening StreamReader?
AStreamReader reads full text
BStreamReader reads partial or no text
CProgram crashes immediately
DStreamWriter closes automatically
💡 Hint
Refer to key_moments about why closing StreamWriter is important before reading.
Concept Snapshot
StreamWriter writes text to files; StreamReader reads text from files.
Always close StreamWriter to save data before reading.
Use WriteLine to write lines; ReadLine to read lines.
Remember to close both to free resources.
Simple way to save and load text data in C#.
Full Transcript
This example shows how to write text to a file using StreamWriter and then read it back using StreamReader in C#. First, StreamWriter opens the file and writes 'Hello World' with WriteLine. Then it is closed to save the data. Next, StreamReader opens the same file and reads the line back into a variable. Finally, StreamReader is closed. Variables track the open/closed state of streams and the text read. Closing StreamWriter before reading is important to ensure data is saved. This process is a basic way to handle text files in C#.