0% found this document useful (0 votes)
2 views

18.1_Files Introduction Slides

The document provides a tutorial on how to write to and read from files in C# using the StreamWriter and StreamReader classes. It explains the steps for writing user input to a file, the importance of closing the file after writing, and how to read the contents of a file line by line. Additionally, it includes exercises and questions to reinforce the concepts covered.

Uploaded by

thikhe03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

18.1_Files Introduction Slides

The document provides a tutorial on how to write to and read from files in C# using the StreamWriter and StreamReader classes. It explains the steps for writing user input to a file, the importance of closing the file after writing, and how to read the contents of a file line by line. Additionally, it includes exercises and questions to reinforce the concepts covered.

Uploaded by

thikhe03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Files

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.

 We are writing information into a notepad document


(very similar to a word document)
STEP 1: Add using System.IO

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Add the line below to your code!


// IO represents InputOutput

using System.IO;
Writing to a File
string name;

StreamWriter myWriter = new StreamWriter("MyFile.txt");

Console.Write("Please enter you name: ");


name = Console.ReadLine();

myWriter.WriteLine("This is my file ...");


myWriter.WriteLine("My name is {0}", 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

Console.Write("Please enter you name: ");


name = Console.ReadLine();

//WriteLine enables a line to the written


myWriter.WriteLine("This is my file ...");
myWriter.WriteLine("My name is {0}", name);

//must close the file in order for lines


//to be saved to the file
myWriter.Close();
Writing to a File
 Where is the file stored?
• In the \bin\Debug folder of the C# project
Writing to a File

StreamWriter myWriter = new StreamWriter("MyFile.txt");

 If a file called MyFile.txt already exists the file will be


opened and anything that gets written to the file will
OVERWRITE the old contents of the file.

 If a file called MyFile.txt does NOT exist it will be


created.
Writing to a File

StreamWriter myWriter = new StreamWriter("MyFile.txt", true);

 If a file called MyFile.txt already exists the file will be


opened and anything that gets written to the file will be
ADDED to the old contents of the file.
 If a file called MyFile.txt does NOT exist it will be
created
Writing to a File
 Use _. Write( ) or _.WriteLine( ) to write to the file.
 Exactly the same as when using Console.Write( ) or
Console.WriteLine( ) BUT nothing appears on the Console
Window.

 Can still use the Console Window to prompt the user for
input or display things to the user.

 VERY IMPORTANT to CLOSE the StreamWriter object,


otherwise NOTHING will be written to the file.
Writing to a File … a summary
string name; Open file
1
StreamWriter myWriter = new StreamWriter("MyFile.txt");

Console.Write("Please enter you name: ");


name = Console.ReadLine();
Write to File
2
myWriter.WriteLine("This is my file ...");

myWriter.WriteLine("My name is {0}", name);

Close file
3
myWriter.Close();
string name;
Writing to a File
StreamWriter myWriter = new StreamWriter("MyFile.txt");

Console.Write("Please enter you name: ");


name = Console.ReadLine();
myWriter.WriteLine("A");
myWriter.WriteLine("B");

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");

Console.Write("Please enter you name: ");


name = Console.ReadLine();
myWriter.WriteLine("A");
myWriter.WriteLine("B");

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]");

for (int i = 1; i <= [D]; i++)


{
Console.Write("Please enter your name: ");
name = Console.ReadLine();
Console.Write("Please enter your surname: ");
surname = Console.ReadLine();
Console.Write("Please enter your age: ");
[E] = int.Parse(Console.ReadLine());

myWriter.[F]([G] + [H] + surname + " " + age);


[I]

[J]

Fill in the missing words …


Solution
string name, surname;
int age;

StreamWriter myWriter = new StreamWriter("ABC.txt");

for (int i = 1; i <= 3; i++)


{
Console.Write("Please enter your name: ");
name = Console.ReadLine();
Console.Write("Please enter your surname: ");
surname = Console.ReadLine();
Console.Write("Please enter your age: ");
age = int.Parse(Console.ReadLine());

myWriter.WriteLine(name + " " + surname + " " + age);


}

myWriter.Close();
 Alternative code…

• You might wonder, if .WriteLine works, do placeholders also


work. Answer is Yes, they do work.

myWriter.WriteLine(name + " " + surname + " " + age);

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.

 Reading from a file - some rules:

• A file MUST exist before you can read from it.

• A file cannot be open for writing AND reading at the


SAME time.

Video clips
Reading from a File
string line;

//using StreamReader class to read from an existing file

StreamReader myReader = new StreamReader("MyFile.txt");


//myReader is the filename in the program, while “Myfile.txt”
is the physical file name on your PC

while (myReader.EndOfStream == false)


{
line = myReader.ReadLine();
Console.WriteLine(line);
}

myReader.Close();
Reading from a File (continued)
string line;

StreamReader myReader = new StreamReader("MyFile.txt");

// loop used to read through all records of the file


// if end of stream is false, it means that more lines need to be
// read in the file
// if end of stream is true, it means that there are no more records
// to read in the file
while (myReader.EndOfStream == false)
{
//ReadLine reads a line in the file
line = myReader.ReadLine();
Console.WriteLine(line);
} //end of loop indicates no more records to be read

myReader.Close(); //file is closed


Reading from a File (continued)
string line; Open file
1
StreamReader myReader = new StreamReader("MyFile.txt");

while (myReader.EndOfStream == false)


2 Loop while
{ it’s false

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");

while (myReader.EndOfStream == false)


{
line = myReader.ReadLine();
Console.WriteLine(line);
}
myReader.Close();
Questions:
1. What class is needed to read from a file?
2. What is the name of the physical file?
3. Where must this physical file be stored?
4. What is the name of the file in this C# program?
5. Must you use a loop to read from the file?
6. The loop continues to execute while .EndOfStream is false. What
does this mean?
7. How do you read from a file?
8. Must you close the file once the loop ends?
9. How does it know that there are no more lines to read in the file?
string line;
StreamReader myReader = new StreamReader("MyFile.txt");
Solution
while (myReader.EndOfStream == false)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
myReader.Close();

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;

StreamReader [A] = new [B]([C]);

while (myReader.EndOfStream == [D])


{
[E] = [F]; //reads the line
Console.WriteLine(line);
}

[G]

Fill in the missing code …


Exercise
string line;

StreamReader myReader = new StreamReader("ABC.txt");

while (myReader.EndOfStream == false)


{
line = myReader.ReadLine(); //reads the ENTIRE line
Console.WriteLine(line);
}

myReader.Close();
Writing AND Reading in the SAME program
string line;

StreamWriter myWriter = new


StreamWriter("MyFile.txt");
myWriter.WriteLine("Just another example");

myWriter.Close();

StreamReader myReader = new


StreamReader("MyFile.txt");
MUST close file
while (myReader.EndOfStream == false)
{
after writing
line = myReader.ReadLine(); BEFORE it can
Console.WriteLine(line); be opened for
}
reading.
myReader.Close();
 Exercises follow

Video clips

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy