File Manipulation Flashcards

1
Q

A class that allows you to open a file for writing. It also allows you to write data to the file using the same print and println methods that you have been using to display data on the screen.

A

PrintWriter class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The name of the file that you wish to open is passed as a what?

A

Passed as a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

To where is the name of the file that you wish to open passed to?

A

To the PrintWriter class’s constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Syntax for creating an empty text file named StudentData.txt and establish a connection between it and the PrintWriter object named outputFile

A

PrintWriter outputFile = new PrintWriter(“StudentData.txt”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where will the file named StudentData.txt be created?
PrintWriter outputFile = new PrintWriter(“StudentData.txt”);

A

The file will be created in the current directory or folder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

TRUE OR FALSE?
When the program is finished writing data to the file, it must close the file.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you write data to a file using the Print Writer?

A

outputFile.println(“Jim”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you make the program close the file after it is finished writing data to the file?

A

outputFile.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

It is what the program throws when an unexpected event occurs in a Java program.

A

Exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens when the main method throws an exception?

A

The program halts and an error message is displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

TRUE OR FALSE?
“PrintWriter objects are capable of throwing exceptions.”

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you allow a method to rethrow an exception that has not been dealt with?

A

Write a throws clause in the method header

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Syntax for writing a throws clause in the method header

A

public static void main(String[] args) throws IOException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What would the output in the StudentData.txt file be?
import java.io*;
public class FileManipulation {
public static void main(String[] args) throws IOException {
PrintWriter outputFile = new PrintWriter(“StudentData.txt”);
outputFile.println(“Jim”);
outputFile.println(“95”);
outputFile.println(“Karen”);
outputFile.println(“98”);
outputFile.println(“Bob”);
outputFile.println(“82”);
outputFile.close();
}
}

A

Jim
95
Karen
98
Bob
82

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the 2 basic steps when writing a program that writes data to a file?

A
  1. You need the import java.io.* statement at the top section of your program.
  2. Create a PrintWriter object and pass the name of the file as a string to the constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Means writing new data to the end of the data that already exists in the file?

A

Appending

17
Q

How many arguments do you need to pass to the FileWriter constructor to append data to an existing file?

A

2

18
Q

Syntax for creating an instance of the FileWriter class and passing 2 arguments to the constructor.

A

FileWriter fwriter = new FileWriter(“MyFriends.txt”, true);

19
Q

Syntax for creating a PrintWriter object to pass a reference to the Filewriter as an argument to the PrintWriter constructor.

A

FileWriter fwriter = new FileWriter(“MyFriends.txt”, true);
PrintWriter outputFile = new PrintWriter(fwriter);

20
Q

Class used to read input from a file.

A

Scanner class

21
Q

Syntax for passing a reference to a File object?

A

File myFile = newFilw(“Customers.txt”);
Scanner inputFile = new Scanner(myFile);

22
Q

How do you close the file using the Scanner class’s close method?

A

inputFile.close();

23
Q

Reads a line of input and returns the line as a string

A

Scanner class’s nextLine method

24
Q

CONTENT OF subject.txt
math
science
english
What would the output be?

CODE
File myFile = new File(“subject.txt”);
Scanner inputFile = new Scanner(myFile);
subject = inputFile.nextLine();
System.out.println(“The first subject is “ + subject);

subject = inputFile.nextLine();

System.out.println(“The second subject is “ + subject);

subject = inputFile.nextLine();

System.out.println(“The third subject is “ + subject);

subject = inputFile.nextLine();

A

OUTPUT
The first subject is math
The second subject is science
The third subject is english

25
Q

What are the 2 arguments that need to be passed to the FileWriter constructor to append data to an existing file?

A
  1. a string containing the name of the file
  2. the boolean value true
26
Q

a signal indicating that the program cannot continue until the unexpected event has been dealt with.

A

Exception