Chapter 10 File I/O

 Multiple Choice )1 An ______allows data to flow into your program. ()a input stream ()b output stream ()c file name ()d all of the above Answer: A

)2 An ______allows data to flow from your program. ()a input stream ()b output stream ()c file name ()d all of the above Answer: B

)3 Files whose contents must be handled as sequences of binary digits are called: ()a text files ()b ASCII files ()c binary files ()d output files Answer: C

)4 The output stream connected to the computer screen is: ()a System.exit ()b System.quit ()c System.in ()d System.out Answer: D

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

1 2 Walter Savitch • Absolute Java 5/e Chapter 10 Test Bank )5 In Java, when you open a text file you should account for a possible: ()a FileNotFoundException ()b FileFullException ()c FileNotReadyException ()d all of the above Answer: A

)6 There are two common classes used for reading from a text file. They are: ()a PrintWriter and BufferedReader ()b FileInputStream and Scanner ()c BufferedReader and Scanner ()d None of the above Answer: C

)7 The scanner class has a series of methods that checks to see if there is any more well-formed input of the appropriate type. These methods are called ______methods: ()a nextToken ()b hasNext ()c getNext ()d testNext Answer: B

)8 All of the following are methods of the Scanner class except: ()a nextFloat() ()b next() ()c useDelimiter() ()d readLine() Answer: D

)9 The method ______reads a single character from an input stream. ()a readLine() ()b skip() ()c read() ()d close() Answer: C

)10 When the method readLine() tries to read beyond the end of a file, it returns the value of: ()a 1 ()b -1 ()c null ()d none of the above Answer: C

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Full file at http://testbanksinstant.eu/ Test-Bank-for-Absolute-Java,-5th- Edition-Walter-Savitch-- )11 A ______path name gives the path to a file, starting with the directory that the program is in. ()a relative ()b descendant ()c full ()d complete Answer: A

)12 The stream that is automatically available to your Java code is: ()a System.out ()b System.in ()c System.err ()d all of the above Answer: D

)13 All of the following are methods of the File class except: ()a exists() ()b delete() ()c getDirectory() ()d getName() Answer: C

)14 The class ObjectOutputStream contains all of the following methods except: ()a writeInt() ()b writeChar() ()c writeDouble() ()d println() Answer: D

)15 The method ______from the File class forces a physical write to the file of any data that is buffered. ()a close() ()b flush() ()c writeUTF() ()d writeObject() Answer: B

)16 The class ObjectInputStream contains all of the following methods except: ()a readLine() ()b readChar() ()c readObject() ()d readInt() Answer: A

download full file at http://testbankinstant.com 4 Walter Savitch • Absolute Java 5/e Chapter 10 Test Bank )17 The read() method of the class RandomAccessFile returns the type: ()a byte ()b int ()c char ()d double Answer: B

 True/False )1 A stream is an object that allows for the flow of data between your program and some I/O device or some file. Answer: True

)2 Every input file and every output file used by your program has only one name which is the same name used by the operating system. Answer: False

)3 The FileNotFoundException is a descendant of the class IOException. Answer: True

)4 When your program is finished writing to a file, it should close the stream connected to that file. Answer: True

)5 Only the classes provided for file output contain a method named close. Answer: False

)6 The methods of the scanner class do not behave the same when reading from a text file as they do when used to read from the keyboard. Answer: False

)7 Using BufferedReader to read integers from a file requires the String input to be parsed to an integer type. Answer: True

)8 A full path name gives a complete path name, starting from the directory the program is in. Answer: False

)9 The File class contains methods that allow you to check various properties of a file. Answer: True

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Full file at http://testbanksinstant.eu/ Test-Bank-for-Absolute-Java,-5th- Edition-Walter-Savitch-- )10 Binary files store data in the same format that is used by any common text editor. Answer: False

)11 Binary files can be handled more efficiently than text files. Answer: True

)12 The preferred stream classes for processing binary files are ObjectInputStream and ObjectOutputStream. Answer: True

 Short Answer/Essay Explain the differences between a text file, an ASCII file and a binary file. Text files are files that appear to contain sequences of characters when viewed in a text editor or read by a program. Text files are sometimes also called ASCII files because they contain data encoded using a scheme known as ASCII coding. Files whose contents must be handled as sequences of binary digits are called binary files.

Write a Java statement to create and open an output stream to a file named autos.txt. PrintWriter outputStream = new PrintWriter(new FileOutputStream("autos.txt"));

Explain what happens when an output file is opened in Java. In Java, when an output stream is connected to a file, the program always starts with an empty file. If the file you are trying to connect to exists, the contents of the file are deleted before the output stream writes new data to the file. If the file you are trying to connect to does not exist, then a new empty file is created.

Write a Java method that returns a String representing a file name entered by the user. Use the BufferedReader class to obtain input.

public static String getFileName() throws IOException

{

BufferedReader stdin = new BufferedReader(new

InputStreamReader(System.in));

System.out.print("Enter the name of the file to open: ");

download full file at http://testbankinstant.com 6 Walter Savitch • Absolute Java 5/e Chapter 10 Test Bank String fileName = stdin.readLine();

return fileName.trim();

}

Use the output stream to the file autos.txt created above in number 2 to write the line “Mercedes” to the file. outputStream.println("Mercedes");

What happens when the method close is invoked on a stream? When the close method is invoked, the system releases any resources used to connect the stream to the file and does any other housekeeping that is needed.

Create try and catch block that opens a file named statistics.txt for output. Writes the integers 24, 55, and 76 to the file, and then closes the file.

PrintWriter outputStream = null;

try

{

outputStream = new PrintWriter(new

FileOutputStream("statistics.txt"));

outputStream.println(24);

outputStream.println(55);

outputStream.println(76);

outputStream.close();

}

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Full file at http://testbanksinstant.eu/ Test-Bank-for-Absolute-Java,-5th- Edition-Walter-Savitch-- catch(FileNotFoundException e)

{

System.out.println("Error opening the file autos.txt");

System.exit(0);

}

Write a Java statement that creates an output stream to append data to a file named autos.txt. PrintWriter outputStream = new PrintWriter(new FileOutputStream("autos.txt", true));

Write a Java statement to create an input stream to a file named autos.txt. Use the BufferedReader class. BufferedReader inputStream = new BufferedReader(new FileReader("autos.txt"));

Write a complete Java program using a BufferedReader object that opens a file named autos.txt and displays each line to the screen.

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileNotFoundException;

import java.io.IOException;

public class TextDemo

{

public static void main(String args[])

{

download full file at http://testbankinstant.com 8 Walter Savitch • Absolute Java 5/e Chapter 10 Test Bank BufferedReader inputStream = null;

try

{

inputStream = new BufferedReader(new FileReader("autos.txt"));

String line = inputStream.readLine();

while(line != null)

{

System.out.println(line);

line = inputStream.readLine();

}

inputStream.close();

}

catch(FileNotFoundException e)

{

System.out.println("Error opening files.");

}

catch(IOException e)

{

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Full file at http://testbanksinstant.eu/ Test-Bank-for-Absolute-Java,-5th- Edition-Walter-Savitch-- System.out.println("Error reading from file.");

}

}

}

Write a Java statement that creates an output stream to a binary file named statistics.dat.

ObjectOutputStream outputStream =

new ObjectOutputStream(new FileOutputStream("statistics.dat"));

Use the output stream created in number 11 above to write the String BBC to the file named statistics.dat. outputStream.writeUTF("BBC");

Write a Java statement to create an input stream to the binary file statistics.dat.

ObjectInputStream inputStream =

new ObjectInputStream(new FileInputStream("statistics.dat"));

Write a complete Java program that opens a binary file containing integers and displays the contents to the screen.

import java.io.ObjectInputStream;

import java.io.FileInputStream;

import java.io.EOFException;

import java.io.IOException;

import java.io.FileNotFoundException;

download full file at http://testbankinstant.com 10 Walter Savitch • Absolute Java 5/e Chapter 10 Test Bank

public class BinaryInputDemo

{

public static void main(String args[])

{

try

{

ObjectInputStream inputStream =

new ObjectInputStream(new

FileInputStream("statistics.dat"));

int stat = 0;

try

{

while(true)

{

stat = inputStream.readInt();

System.out.println(stat);

}

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Full file at http://testbanksinstant.eu/ Test-Bank-for-Absolute-Java,-5th- Edition-Walter-Savitch-- }

catch(EOFException e)

{

System.out.println("End of file encountered");

}

inputStream.close();

}

catch(FileNotFoundException e)

{

System.out.println("Unable to locate file");

}

catch(IOException e)

{

System.out.println("Unable to read file");

}

}

}

Write a Java statement that creates a stream that provides read/write access to the file named autos.txt. RandomAccessFile ioStream = new RandomAccessFile("autos.txt", "rw");

download full file at http://testbankinstant.com 12 Walter Savitch • Absolute Java 5/e Chapter 10 Test Bank )1 Write a Java statement to create an input stream to a file named “statistics.dat”. )2 Scanner inputStream = new Scanner(new FileReader("statistics.dat"));

)3 Write a complete Java program using a Scanner object that opens a file named autos.txt and displays each line to the screen. )4

import java.util.*;

import java.io.FileReader;

import java.io.FileNotFoundException;

public class TextDemo

{

public static void main(String args[])

{

Scanner inputStream = null;

try

{

inputStream = new Scanner(new FileReader("autos.txt"));

String line = inputStream.nextLine();

while(line != null)

{

©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Full file at http://testbanksinstant.eu/ Test-Bank-for-Absolute-Java,-5th- Edition-Walter-Savitch-- System.out.println(line);

line = inputStream.nextLine();

}

inputStream.close();

}

catch(FileNotFoundException e)

{

System.out.println("Error opening files.");

}

}

}

download full file at http://testbankinstant.com