18.1_Files Introduction Slides
18.1_Files Introduction Slides
Video clips
LECTURE OBJECTIVES
Understand how to write to a file
Understand how to read from a file
The slides that follow will explain how to write
information to a file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
Writing to a File
string name;
myWriter.Close();
Writing to a File: Explanation
string name;
//physical file name
//found in PC
//must make use of StreamWriter class to write to a file
StreamWriter myWriter = new StreamWriter("MyFile.txt");
//myWriter is the filename
//which is used in the program
Can still use the Console Window to prompt the user for
input or display things to the user.
Close file
3
myWriter.Close();
string name;
Writing to a File
StreamWriter myWriter = new StreamWriter("MyFile.txt");
myWriter.Close();
Questions:
1. What is the name of the physical file found in memory?
2. What is the name of the file that you are using in your program, to allow you
to write to the file?
3. What class must be used to allow writing to a file?
4. Can you use both .WriteLine and .Write to write to a file?
5. Is it important to close the file after writing to it? (yes/no). If yes, why is
that?
6. What will be written to this file?
7. Where is this file found – which folder/directory?
string name;
Writing to a File
StreamWriter myWriter = new StreamWriter("MyFile.txt");
myWriter.Close();
Questions:
1. What is the name of the physical file found in memory? MyFile.txt
2. What is the name of the file that you are using in your program, to allow you
to write to the file? myWriter
3. What class must be used to allow writing to a file? StreamWriter
4. Can you use both .WriteLine and .Write to write to a file? Yes
5. Is it important to close the file after writing to it? (yes/no). If yes, why is
that? Yes, important, can only save what has been written, if you close the
file
6. What will be written to this file? A
B
7. Where is this file found – which folder/directory?
in the bin/debug folder of the project you are working on
Exercise
string name, surname;
int age;
StreamWriter [A] = new [B] ("[C]");
[J]
myWriter.Close();
Alternative code…
OR
myWriter.WriteLine("{0} {1} {2}", name, surname, age);
Reading from a File
The slides that following explain how to read
information from a file.
Video clips
Reading from a File
string line;
myReader.Close();
Reading from a File (continued)
string line;
line = myReader.ReadLine(); 3
Read line in loop
Console.WriteLine(line);
}
Must use a LOOP
myReader.Close(); 4 to read the contents
Close file of the file line by
line.
Exercise
string line;
StreamReader myReader = new StreamReader("MyFile.txt");
Questions:
1. What class is needed to read from a file? StreamReader
2. What is the name of the physical file? MyFile.txt
3. Where must this physical file be stored? In bin\debug folder of
project you are busy with
4. What is the name of the file in this C# program? myReader
5. Must you use a loop to read from the file? yes
6. The loop continues to execute while .EndOfStream is false. What
does this mean? False means there are more records/lines to read
7. How do you read from a file? ReadLine
8. Must you close the file once the loop ends? yes
9. How does it know that there are no more lines to read in the file?
automatically detects, and .EndOfStream becomes true, so the
loop ends
Exercise
string line;
[G]
myReader.Close();
Writing AND Reading in the SAME program
string line;
myWriter.Close();
Video clips