File IO Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is TextInputFile?

A

storage command provided by arc library to read files

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

What is TextOutputFile?

A

storage command provided by arc library to write files

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

It is good practice to…

A

close your txt files after using them

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

Function of:

TextInputFile variablename = TextInputFile(“filename.txt”);

A

Storage Command
* tells computer to open up the specific file from the hard drive/storage
* gives the file name a variable which can be used throughout program

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

How do you check to see if your code can open the file?

A

if no error message appears in the background window

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

Does variable name for TextInputFile or TextOutputFile have to match the text file?

A

No

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

Function of Storage Input Command

A

tells the computer to read a line from the already opened file

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

tells the computer to read a line from the already opened file

A

Function of Storage Input Command

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

Write the line of code to open text file for reading

A

TextInputFile variablename = new TextInputFile(“textfilename.txt”);

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

Write the line of code to open a text file for writing

A

TextOutputFile variablename = new TextOutputFile(“textfilename.txt”);

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

Write an example of a Storage Input Command for any variable type

A

strLine = txtvariablename.readLine();

intLine = txtvariablename.readInt();

dblLine = variablename.readDouble();

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

Write a line of code to close the opened text file

A

textvariablename.close();

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

Function of .eof()

A

Method which means “End of File”
* looks for a blank line b/w lines or at the end of text and identifies it as the end of code

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

Write the general structure to read every line of code in a file (as a string)

A

while(textvariablename.eof() == false) {
strLine = textvariablename.readLine();
}

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

What is it good practice to do when finished with a file?

A

to close it

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

If file format is:
* Course Name
* Course Mark
* Course Name
* Course Mark …

How do you print just the course name?

A
  1. Read all lines
  2. Print just the course name variable
17
Q

Function Storage Output Command

A

tells computer to print whatever is in quotes or in the variable to the opened text file

18
Q

Write an example of a Storage Output Command

A

filevariablename.println(“quotes” + intVariable);

19
Q

Function of:

TextOutputFile variablename = new TextOutputFile(“filename.txt”);

A

Storrage Command
* tells hard drive/storage to create a new text file (or clear and edit existing file) called filename.txt for writing

20
Q

Function of:

TextOutputFile variablename = new TextOutputFile(“filename.txt”, true);

A

Storage Command
* tells hard drive/storage to create a new text file (or edit existing file) called filename.txt for writing
* any new data will be appended (added) to the bottom of the opened text file

21
Q

Function of “, true”

A

Appends any new data to the bottom of the text file

22
Q

Write a program that will read every line from the file canadapm.txt but only print all the data for a specific Prime Minister.
Example: If the user types “Pierre Trudeau,” the program should only display data related to Pierre Trudeau.

A

import arc.*;

public class iohw4{
public static void main(String [] args){
Console con = new Console();
// Read every line
// Print data from specific prime minister that user chooses

	// Open txt
	TextInputFile canadapm = new TextInputFile("canadapm.txt");
	
	// Variables
	String strPM;
	int intBirthYr;
	int intDeathYr;
	int intElectYr;
	int intResignYr;
	String strParty;
	String strInput;
	
	// Input
	con.println("This file contains data on all the Canadian prime ministers.");
	con.print("Choose a Prime Minister to learn more about: ");
	strInput = con.readLine();
	
	// Read & Print
	while(canadapm.eof() == false){
		strPM = canadapm.readLine();
		intBirthYr = canadapm.readInt();
		intDeathYr = canadapm.readInt();
		intElectYr = canadapm.readInt();
		intResignYr = canadapm.readInt();
		strParty = canadapm.readLine();
		
		if(strPM.equalsIgnoreCase(strInput)){
			con.println("Birth year: " + intBirthYr);
			if(intDeathYr == 0){
				con.println("Alive :)");
			}else{
				con.println("Death year: " + intDeathYr);
			}
			con.println("Elected in: " + intElectYr);
			if(intResignYr == 0){
				con.println("Still Active");
			}else{
				con.println("Resigned in: " + intResignYr);
			}
			con.println("Party: " + strParty);
		}
	}
	canadapm.close();
			
} }
23
Q

Write a program that reads every name from the file bff.txt and prints them to the screen.
After reading, open the file for writing, ask the user for one more friend’s name, and append it to the end of the file.

A

import arc.*;

public class iohw6{
public static void main(String [] args){
Console con = new Console();

	// Read every name from bff.txt
	// Print names
	// add one more name (without erasing)
	
	//open filee
	TextInputFile bff = new TextInputFile("bff.txt");
	
	//variables
	String strFriend;
	
	con.println("These are the people currently on your friends list");
	while(bff.eof() == false){
		strFriend = bff.readLine();
		con.println(strFriend);
	}
	bff.close();
	
	
	TextOutputFile addbff = new TextOutputFile("bff.txt", true);
	//add name
	con.print("Add a name to your friends list: ");
	strFriend = con.readLine();
	addbff.println(strFriend);
	
	addbff.close();
	
	//do I have to close both? 
		//close txt after using
	
	con.println();
	con.println("added!");
	
	
} }
24
Q

Write a program that asks for the names of your best friends and writes them to a file called bff.txt.
Stop writing names and close the file when the user types “stop.”

A

import arc.*;

public class iohw5{
public static void main(String [] args){
Console con = new Console();

	// write names of friends to a file called bff.txt
	// stop adding names when user types stop
	// close file
	
	TextOutputFile addfriend = new TextOutputFile("bff.txt");
	
	String strFriendo = "";
	
	while(!strFriendo.equalsIgnoreCase("stop")){
		con.println("Who would you like to add to your friend's list? (Enter \"stop\" to end program)");
		strFriendo = con.readLine();
		
		addfriend.println(strFriendo);
		con.println();
	}
	
	addfriend.close();
	con.println("closed bff.txt");
} }