READING Java Input and Output *** THIS FIRST PART IS BASICALLY REVIEW *** - input - any information needed by your program to complete execution - forms - graphical, e.g. popup dialog - clicking mouse - from file - from network connection - from devices (e.g. USB, camera, scanner, mike) - output - information that the program must convey to the user - forms - what view on screen - print out created by a printer - sound from speakers - I/O so far - input: BufferedReader plus System.in - output: PrintWriter via System.out - errors while getting input - unexpected data format - e.g. letters instead of numbers - causes exception unless program designed to handle - handle invalid input by only accepting valid input - not done by Java classes - gain flexibility at the cost of more complexity - console input - any input entered in the console window - console window: black window automatically launched when CodeWarrior run - prompting the user - telling the user that s/he needs to provide input - formats - dialog box - program frame - console window - required if program needs to get info from user - don't count on blinking cursor - need to tell user what kind of input needed anyway - java classes needed to successfully receive input from the user - mostly contained in the java.io package - major classes that we will use - to use, either - import java.io or - use the fully qualified names - java.io.InputStream - store info about the connection btw an input device and the program - java.io.InputStreamReader - translate data bytes received from an InputStream object into characters - java.io.BufferedReader - buffer/store input received from an InputStreamReader object - improves efficiency by reducing the number of times the program has to query the device for input - using these classes to get input - use BufferedReader instance method readLine - initialize the BufferedReader with an instance of java.io.Reader - InputStreamReader extends Reader, so we can just use InputStreamReader - initialize the InputStreamReader class with an instance of InputStream - java.lang.System class - automatically included with every class - includes an instance of InputStream connected to the keyboard - System.in - steps for console based user input - Use System.in to create an InputStreamReader object - Use InputStreamReader object ot create a BufferedReader object - Display a prompt to the user for the desired data - Use the BufferedReader object to read a line of text from the user - Use the input - readLine may throw a checked exception - must either catch the exception or add throws IOException to the declaration of the method that calls readLine - integer input - readLine always returns a String - if a different type is desired, must parse (convert) the String to the desired type - parsing a String into an int - get a String of integer format via readLine - user the class method Integer.parseInt to parse the String - provide the String to parse as the sole argument - method returns the int to which the String is equivalent - if the String cannot be converted to an int, parseInt throws a NumberFormatException - Integer class is a wrapper class - many other wrapper classes - wrapper classes are used for - parsing - storing a primitive as an object - console output - important alternative to graphical, which may not be readily supported by some environments - display a string of characters to some standard display device (usually the screen) - called console output b/c chars usually appear in the console window - System.out.print and System.out.println - System.out is an instance of the PrintStream class, which extends the Stream class - streams - instance of Stream class - store info needed to connect a program to an input or output device - Printer object adds functionality to output streams - PrintStream class - extends Printer - contains definitions for all of the versions of print and println - automatically created by java.lang.System when the application begins execution - public and static - InputStream: System.in - PrintStream: System.out, System.err - print and println work with any type of data - implemented as overloaded methods - use the toString conversion of whatever object is being printed - can overload the toString method for objects that you define so that printing them generates some logical output *** THIS IS WHERE THE NEW STUFF BEGINS *** - file input - reading files - attach a BufferedReader object to a FileReader instead of an InputStreamReader - assume text file with valid ASCII data - necessary classes - java.io.File - stores info about a file on a drive - constructor takes a String argument (the name of the file) - instance method exists returns whether that file exists, as a boolean - java.io.FileReader - translate file data bytes into a stream of characters - constructor takes a File object - close method closes the file stream - readLine returns null at the end of a file - must either propagate or catch associated IOException - string tokenizer - parse files to use as data (e.g., creating new objects) - delimiter: special character used to separate data fields - tokenizing: splitting a line of text into different parts - token: each piece of the split line - java.util.StringTokenizer class - constructor takes two objects, the first one a string representing the data to be parsed, and the second a string representint the delimiter - hasMoreTokens method determines whether there are more tokens to process - nextToken method returns the next token as a String - will throw a NoSuchElementException if called when no more tokens are left - countTokens method returns the number of remaining tokens - file output - writing to a file - file already exists - behavior - overwrite - different file name - cancel - append - test for this: use File instance method exists() - classes - java.io.FileWriter - used to - open a file - connect an output stream to a file - constructor takes the name of the file to create , and a boolean of whether or not to append (overwrites if false) - java.io.PrintWriter - write strings of characters to a Writer object - constructor takes a Writer object and a boolean that says whether or not the data should be flushed (or force-written) to the file whenever println is used - print method takes a String to write to the file - this is buffered by default - means that it is not actually written to the file until the buffer is explictly flushed - flush method moves data from the buffer to the file - println method also takes a String to write to the file, but may automatically flush if requested initially in the constructor - close method required to ensure all of the data is saved properly - more efficient to wait to flush until done writing to buffer