Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Beginning Java with NetBeans: Chapter 17 How to work with file I/O MULTIPLE CHOICE [Answers are in tables delete all but the correct answers cell]

Beginning Java with NetBeans: Chapter 17

How to work with file I/O

MULTIPLE CHOICE [Answers are in tables delete all but the correct answers cell]

What package that was introduced with Java 7 provides an improved way to access the file system?

a.

java.file

c.

java.io

b.

java.nio.file

d.

java.file.system

What operation is not provided by the Paths, Path, and Files classes?

a.

Listing files in a directory

c.

Reading the contents of a file

b.

Listing subdirectories of a directory

d.

Creating a new file

What does the following code do?

String dirString = "c:/files/"; String fileString = "products.txt"; Path path = Paths.get(dirString + fileString); if (Files.notExists(path)) { Files.createFile(path); }

a.

It creates the directory at the specified path if that directory doesnt already exist.

b.

It creates the directory at the specified path even if that directory already exists.

c.

It creates the file at the specified path even if that file already exists.

d.

It creates the file at the specified path if that file doesnt already exist.

What static method of the Files class returns a DirectoryStream object that you can use to loop through all files and subdirectories of a directory?

a.

getDirectoryPaths()

b.

newDirectoryStream()

c.

getPaths()

d.

listPaths()

Which of the following statements create a file named test.txt in the root directory of the C drive?

a.

Path p = new Path("c:/test.txt"); p.createFile();

b.

Files.createFile("c:/test.txt");

c.

Path filePath = Paths.get("c:/test.txt"); Files.createFile(filePath);

d.

File f = new File(); f.createNewFile("c:/test.txt");

From what abstract class are all classes that are used to write text files derived?

a.

OutputStreamWriter

c.

Writer

b.

PrintWriter

d.

FileWriter

What type of stream must be used to send data from an application to a file?

a.

input stream

c.

character stream

b.

output stream

d.

binary stream

What is it called when data is stored in a block of memory before being moved to a file?

a.

buffering

c.

layering

b.

flushing

d.

queuing

Which exception class do all file handling exceptions derive from?

a.

FileException

c.

FileIOException

b.

IOException

d.

DataException

To write to a file more efficiently, you should use a

a.

FileReader stream

c.

BufferedWriter stream

b.

File stream

d.

FileWriter stream

If the variable named in represents an input stream object, what type of exception is prevented by the while statement in the code that follows?

String word = in.readLine(); while(word != null) { word = in.readLine(); }

a.

DataException

c.

FileNotFoundExeption

b.

EOFException

d.

NullPointerException

What type of exception can be prevented by testing the exists or notExists method of the Files class?

a.

DataException

c.

FileNotFoundExeption

b.

EOFException

d.

NullPointerException

The main purpose of the PrintWriter class is to connect

a.

a character output stream to a file

b.

an application to a character output stream

c.

a buffer to a file

d.

a character output stream to a binary output stream

What does the second argument in the PrintWriter constructor that follows mean?

PrintWriter writer = new PrintWriter( new FileWriter("file.txt"), true);

a.

Data can be appended to this file.

b.

Data is flushed from the buffer each time a println statement is executed.

c.

If the file doesnt exist, one should be created.

d.

The file should be overwritten.

What happens when the following statements are executed?

PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter("test.txt"))); String s = "xyz"; out.println(s); out.close();

a.

The string xyz is written to the test.txt file.

b.

The address of the string named s is written to the file.

c.

An IOException is thrown.

d.

The string xyz is displayed on the console.

In the code that follows, what delimiter is used to separate each row?

for (int i = 0; i < vals.length; i++) { out.print(vals[i]); out.print("\t"); out.println(nums[i]); }

a.

a slash and the letter t

c.

a new line

b.

a tab

d.

a comma

The main purpose of the BufferedReader class is to connect

a.

a character input stream to an application

b.

a file to a character input stream

c.

a binary input stream to a character input stream

d.

a file to an application

In the code that follows, what methods can the object named in call?

BufferedReader in = new BufferedReader( new FileReader("dat.txt"));

a.

Methods of the BufferedReader class

b.

Methods of the BufferedReader and FileReader classes

c.

Methods of the FileReader class

d.

Methods of the BufferedFileReader class

What exception is thrown by methods of the BufferedReader class?

a.

NullPointerException

c.

IOException

b.

FileException

d.

BufferFullException

What data from the b.txt file does the readLine method return in the code that follows?

Path p = Paths.get("b.txt"); BufferedReader in = new BufferedReader( new FileReader(p.toFile())); String line = in.readLine();

a.

The entire file

c.

The name of the file

b.

The first line of the file

d.

The first character in the file

Code example 17-1

package murach.test; import java.io.*; import java.nio.file.*; public class TestFileReader { public static void main(String[] args) { Path p = Paths.get("a.txt"); try (BufferedReader in = new BufferedReader( new FileReader(p.toFile()))) { String line = in.readLine(); while (line != null) { String[] v = line.split("::"); String lname = v[0]; String fname = v[1]; String dept = v[2]; System.out.println(fname + " " + lname + ", dept " + dept); line = in.readLine(); } } catch (FileNotFoundException e) { System.out.println("File not found."); } catch (IOException e) { System.out.println("I/O error."); } } }

(Refer to code example 17-1.) What delimiter is used to separate fields in the a.txt file?

a.

a backslash

c.

two tabs

b.

two colons

d.

a comma

(Refer to code example 17-1.) How many fields are extracted from each line?

a.

1

c.

3

b.

2

d.

not enough information to determine

(Refer to code example 17-1.) What will happen if the file a.txt does not exist?

a.

The readLine method will throw a FileNotFoundException.

b.

The FileReader constructor will throw a FileNotFoundException.

c.

The BufferedReader constructor will throw a FileNotFoundException.

d.

The File constructor will throw a FileNotFoundException.

COMPLETION

The _______________ package contains the file handling classes such as PrintWriter and BufferedReader.

The _____________ class represents a path to a file or a directory.

To read and write a text file, you use a _______________ stream.

The ____________________ class provides buffering for output to a text file.

The ____________________ class is the abstract class thats implemented by the BufferedReader and InputStreamReader classes.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books

Students also viewed these Databases questions

Question

Compare and contrast SWOT analysis with portfolio analysis.

Answered: 1 week ago

Question

=+describe the nine compositional modes needed for electronic media

Answered: 1 week ago