Modulo 11: How to work with file I/O
If the numbersFile string refers to a file that contains three integers, how many integers will that file contain when the code that follows is executed? BinaryWriter binaryOut = new BinaryWriter( new FileStream(numbersFile, FileMode.Create)); binaryOut.Write(4); binaryOut.Close();
1
What is the following code an example of? using (StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create))) { // code that uses the textOut object goes here }
A using statement
Which class is used to work with directories (folders)?
Directory
Assume that you have a valid StreamReader object named textIn. What does the following code do? while (textIn.Peek() != -1) { string row = textIn.ReadLine(); MessageBox.Show(row); }
Displays each line of a text file in successive message boxes
If a string named path refers to a file that you want to delete, which of the following statements will delete that file?
File.Delete(path);
What type of exception can be prevented by using the Exists() method of the File class?
FileNotFoundExeption
Which class is the base class for exceptions that are thrown during the processing of a stream, file, or directory?
IOException
What type of exception is prevented by the use of the if statement in the finally block that follows? finally { if (fileStream != null) { fileStream.Close(); } }
NullPointerException
If the path variable contains a string that refers to an existing file, what does the following code do? FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
Opens the file
If the path variable contains a string that refers to an existing file, what does the following code do? FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
Opens the file so data can be written to it, but not read from it
Which of the following is not a common I/O exception?
PathTooLongException
What method of the BinaryReader class do you use to read an int value from a binary file?
ReadInt32()
To read a text file, you need to use a FileStream object and a
StreamReader object
What namespace does .NET use to store classes that work with input and output operations?
System.IO
Assume that you have a valid StreamWriter object named textOut. What does the following code do? textOut.Write("Wizard of Oz"); textOut.Write("\t"); textOut.WriteLine("1939");
Writes a record that consists of two tab-delimited fields to a text file
To allow data to flow from a file to an application's internal memory, .NET uses
an input stream
Compared to a using declaration, the advantage of a using statement is that it
gives you more control over specifying the scope of the object that has resources that need to be released when the object goes out of scope
When you use a method to read data from a text file, you normally read
one line
The primary difference between a text file and a binary file is that a binary file
stores data with different data types
When you call the Write() method of a binary output stream, you must specify
the data to be written