LECTURE 12 NOVEMBER 2004 ASSIGNMENTS Today 11:58 PM CodeLab #10 Mon 11/22 Assignment 4 STRING TOKENIZER Why? - what to do with the file once reading it - may want to interpret the data in special ways - very common way: databases or spreadsheets -- creating new objects from the data in the file What? - delimiter: special character used to separate data fields - tokenizing - splitting a line of text into different parts (tokens) - throws away the delimiter How? - java.util.StringTokenizer class - constructor - arguments 1. String of the data to be parsed 2. String of all delimiters - (optional) default delimiters: space, tab, newline, return - 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 Example: System.out.println("Enter a line with integers: "); String input = stdin.readLine(); StringTokenizer st; String delims = " ,;\t"; st = new StringTokenizer( input, delims ); while ( st.hasMoreTokens() ) { int n = Integer.parseInt(st.nextToken()); System.out.println(n); } FILE OUTPUT What? - writing to a file What? - if the file already exists How? - File instance method exists() Options? - overwrite - different file name - cancel - append How? - similar to BufferedReader, but in the opposite direction - output stream: stream of data to output from your program What? - open a file - connect an output stream to a file How? - java.io.FileWriter - constructor takes the name of the file to create , and a boolean of whether or not to append (overwrites if false) What? - write strings of characters to a Writer object - use a FileWriter to write to a file How? - java.io.PrintWriter - 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 Example: // Create a FileWriter attached to a file named "out.txt". // The second parameter sets whether or not new data // will be appended to the end of the file or the beginning. // false means that this file will be overwritten. java.io.FileWriter fw = new java.io.FileWriter( "out.txt", false ); // Create a PrintWriter that automatically flushes data // to the output file whenever the println method is used. java.io.PrintWriter pw = new java.io.PrintWriter( fw, true ); // Buffer some data to write to the file (doesn't actually write until flush) pw.print( "Some test data that will be written when flush is called."); // Flush all buffered data to the file. pw.flush(); // Write some data and automatically flush it to the file. pw.println( data ); // Close the PrintWriter for added safety. pw.close(); FILE IO CUMULATIVE EXAMPLE - java.io.File: additional useful methods - instance method isFile returns whether or not the object is a file (as opposed to being a directory) - instance method isDirectroy returns whether or not the object is a directory - instance method canRead returns whether or not the program can read the file (it might not have security permissions) - instance method canWrite returns whether or not the program can write the file - instance method ready() returns whether or not there are still characters in the file to be read