File IO Flashcards
What is TextInputFile?
storage command provided by arc library to read files
What is TextOutputFile?
storage command provided by arc library to write files
It is good practice to…
close your txt files after using them
Function of:
TextInputFile variablename = TextInputFile(“filename.txt”);
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 do you check to see if your code can open the file?
if no error message appears in the background window
Does variable name for TextInputFile or TextOutputFile have to match the text file?
No
Function of Storage Input Command
tells the computer to read a line from the already opened file
tells the computer to read a line from the already opened file
Function of Storage Input Command
Write the line of code to open text file for reading
TextInputFile variablename = new TextInputFile(“textfilename.txt”);
Write the line of code to open a text file for writing
TextOutputFile variablename = new TextOutputFile(“textfilename.txt”);
Write an example of a Storage Input Command for any variable type
strLine = txtvariablename.readLine();
intLine = txtvariablename.readInt();
dblLine = variablename.readDouble();
Write a line of code to close the opened text file
textvariablename.close();
Function of .eof()
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
Write the general structure to read every line of code in a file (as a string)
while(textvariablename.eof() == false) {
strLine = textvariablename.readLine();
}
What is it good practice to do when finished with a file?
to close it
If file format is:
* Course Name
* Course Mark
* Course Name
* Course Mark …
How do you print just the course name?
- Read all lines
- Print just the course name variable
Function Storage Output Command
tells computer to print whatever is in quotes or in the variable to the opened text file
Write an example of a Storage Output Command
filevariablename.println(“quotes” + intVariable);
Function of:
TextOutputFile variablename = new TextOutputFile(“filename.txt”);
Storrage Command
* tells hard drive/storage to create a new text file (or clear and edit existing file) called filename.txt for writing
Function of:
TextOutputFile variablename = new TextOutputFile(“filename.txt”, true);
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
Function of “, true”
Appends any new data to the bottom of the text file
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.
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(); } }
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.
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!"); } }
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.”
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"); } }