File I/O No class on Friday, November 26 CS Cookie Party!!! - - PDF document

file i o
SMART_READER_LITE
LIVE PREVIEW

File I/O No class on Friday, November 26 CS Cookie Party!!! - - PDF document

Reminders... Assignment #9 is special... No lab on Wednesday, November 24 File I/O No class on Friday, November 26 CS Cookie Party!!! Friday, December 10 4:30 - 7:30pm, Lake House Kitchen T- 1 T- 2 Election 2008 Writing a


slide-1
SLIDE 1

T- 1

File I/O

T- 2

Reminders...

  • CS Cookie Party!!!

Friday, December 10 4:30 - 7:30pm, Lake House Kitchen

  • Assignment #9 is special...
  • No lab on Wednesday, November 24
  • No class on Friday, November 26

T- 3

  • Electronic voting is

very much in the news.

  • After the citizens vote,

the results must be written to a file for safe keeping.

  • How to do this?

Election 2008

T- 4

import java.io.*; If the file “votes.txt” does not exist, FileWriter will create it FileWriter lives in java.io package The write() method takes a String Tidy up when done

Writing a String to a text file

class WriteAFile { public static void main (String[] args) { } } FileWriter writer = new FileWriter (”votes.txt"); writer.write( ”George W. Bush\n” ); writer.write( ”John F. Kerry\n” ); writer.close();

T- 5

Risky business

T- 6

Methods in java use exceptions to tell the calling code, “Something bad happened. I failed.”

The contract tells us that write() can throw an exception.

When good programs go bad

slide-2
SLIDE 2

T- 7

  • The compiler needs to

know that YOU know you’re calling a risky method.

  • If you wrap the risky

code in something called a try/catch, the compiler will relax.

Humoring Java

T- 8

*Of type Exception. IOException Exception Throwable printStackTrace() getmessage()

try { // do risky thing } catch (Exception ex) { // try to recover // block runs only // if Exception is // thrown }

An Exception is an Object*

T- 9

import java.io.*; class WriteAFile { public static void main (String[] args) { try { FileWriter writer = new FileWriter (”votes.txt"); writer.write(”George W. Bush\n”); writer.write(”John F. Kerry\n”); writer.close(); } catch (IOException ex) { System.out.println(“Voter error. Inform Supreme Court.”); ex.printStackTrace(); } } }

Guarding against failure

T- 10

import java.io.*; class WriteAFile { public static void main (String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter (”votes.txt” )); writer.write(”George W. Bush\n”); writer.write(”John F. Kerry\n”); writer.close(); } catch (IOException ex) { System.out.println(“Voter error. Inform Supreme Court.”); ex.printStackTrace(); } } } We chain a BufferedWriter to FileWriter for more efficient I/O.

BufferedWriter

T- 11

File I/O without buffers is like shopping without a cart.

“George W Bush” “John F. Kerry” “George W. Bush” “George W Bush John F Kerry” George W Bush John F.Kerry

String is written to BufferedWriter (chain stream that works with characers) is chained to FileWriter (connection stream that writes characters as opposed to bytes) File When the buffer is full, the Strings are all written to String is put into a buffer with

  • ther Strings

The beauty of buffers

T- 12

Reading from a text file

import java.io.*; class ReadAFile { public static void main (String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader(“votes.txt”)); reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } FileReader is a connection stream for characters connecting to text file Chain fileReader to Buffered Reader for more efficient reading String line = null; // Variable to hold each input line while ((line = reader.readLine()) != null) { System.out.println(line); }

slide-3
SLIDE 3

T- 13

A magic eight ball

  • We write an “eight ball”

program that answers yes/no questions.

  • The eight ball keeps responses

like

Without a doubt. It seems unlikely. My sources say no. It is certain. Concentrate and ask again.

in a file named answers.txt

T- 14

Magic8Ball

import java.awt.*; import java.applet.*; public class Magic8Ball { public static void main (String[] args) { System.out.println(StringChooser.chooseLine("answers.txt”)); } } StringChoose.chooseLine reads responses from “answers.txt”, then randomly returns one

T- 15

First we load our answers

import java.io.*; import java.util.ArrayList; public class StringChooser { public static String chooseLine (String inFile) { try { BufferedReader reader = new BufferedReader(new FileReader(“votes.txt”)); // But where do we put them all? } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } return ”Ask me again tomorrow"; // Got to return something } }

T- 16 Without a doubt It seemly unlikely

1 MAXANS private static final MAXANS = 10; String [] myArray = new String[MAXANS]; myArray …

My sources say no

2 *Why? size 6

An array might be nice*

T- 17

public class StringChooser { private static final MAXANS = 10; // Max number of answers allowed String [] myArray = new String[MAXANS]; // Stuff into a String array public static String chooseLine (String inFile) { try { BufferedReader reader = new BufferedReader(new FileReader(“votes.txt”)); String line = null; int size = 0; while ((line = reader.readLine()) != null) { myArray[size] = line; size++; } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } return ”Ask me again tomorrow"; } }

Stuffing the answers into an array

T- 18

Focus please!

… private static final MAXANS = 10; // Max number answers allowed String [] myArray = new String[MAXANS]; … String line = null; int size = 0; while ((line = reader.readLine()) != null) { myArray[size] = line; size++; }

Without a doubt It seemly unlikely

1 MAXANS myArray …

My sources say no

2 size 6

slide-4
SLIDE 4

T- 19

Avoiding array out of bounds

… private static final MAXANS = 10; // Max number answers allowed String [] myArray = new String[MAXANS]; … String line = null; int size = 0; while (((line = reader.readLine()) != null) && (size < MAXANS)){ myArray[size] = line; size++; }

Without a doubt It seemly unlikely

1 MAXANS myArray …

My sources say no

2 size 6

T- 20

Rolling the dice

return (int)(Math.random() * size);

Returns a random integer i where 0 ≤ i < size-1 Returns a random double d where 0.0 ≤ d < 1.0

return Math.random();

Returns a random double d where 0.0 ≤ d < size

return Math.random() * size;

Returns a random String from myArray

return myArray[(int)(Math.random() * size)];

T- 21

Putting it all together

public class StringChooser { private static final MAXANS = 10; // Max number of answers allowed public static String chooseLine (String inFile) { try { BufferedReader reader = new BufferedReader(new FileReader(“votes.txt”)); String [] myArray = new String[MAXANS]; String line = null; int size = 0; while (((line = reader.readLine()) != null) && (size < MAXANS)) { myArray[size] = line; size++; } reader.close(); return myArray[(int)(Math.random() * size)]; } catch (Exception ex) { ex.printStackTrace(); } return ”Ask me again tomorrow"; } }