week 14 - monday. what did we talk about last time? inheritance

20
CS 121 Week 14 - Monday

Upload: adele-hodge

Post on 17-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 14 - Monday.  What did we talk about last time?  Inheritance

CS 121Week 14 - Monday

Page 2: Week 14 - Monday.  What did we talk about last time?  Inheritance

Last time

What did we talk about last time? Inheritance

Page 3: Week 14 - Monday.  What did we talk about last time?  Inheritance

Questions?

Page 4: Week 14 - Monday.  What did we talk about last time?  Inheritance

Project 5

Page 5: Week 14 - Monday.  What did we talk about last time?  Inheritance

Files

Page 6: Week 14 - Monday.  What did we talk about last time?  Inheritance

Files in Java

In this course, you have already dealt with image and audio files, but you didn't do any direct input or output to them

It is possible to read and write individual pieces of data to a file

Files are great because they exist after the program is done

Reading and writing to a file is very similar to reading and writing to the command line (using Scanner and System.out)

Page 7: Week 14 - Monday.  What did we talk about last time?  Inheritance

Reading

Reading from a text file is almost ridiculously easy

We use Scanner, just like reading from the command line

We just have to create a new File object that gives the file we want to read from

This code will read from some file called input.txt, as if someone were typing its contents into the command line

Scanner in = new Scanner(new File("input.txt"));

Page 8: Week 14 - Monday.  What did we talk about last time?  Inheritance

I take exception to that…

Unfortunately, if you type that into Eclipse, you'll get a red squiggle underneath the code

The problem is this: What would happen if input.txt doesn't exist?

This is an error situation, and Java uses something called exceptions to deal with errors

You can catch an exception and do something to recover from the situation

Page 9: Week 14 - Monday.  What did we talk about last time?  Inheritance

Checked exceptions

However, the error if the file isn't there is called a FileNotFoundException, and it's a checked exception

If there is the possibility of throwing a checked exception, your code has to deal with it or else your program will not compile

Well, that's annoying: Now we have to learn how to deal with catching exceptions

Page 10: Week 14 - Monday.  What did we talk about last time?  Inheritance

Unchecked exceptions

You've seen exceptions before: NullPointerException ArrayIndexOutOfBoundsException etc.

These are called unchecked exceptions, because you don't have to deal with them

You usually can't deal with them: They mean that you're program is messed up

Page 11: Week 14 - Monday.  What did we talk about last time?  Inheritance

Throw them 'bows!

The alternative to catching an exception is throwing it up to the next level, making it someone else's problem

Sure, your program will crash if no one deals with it, but at least your code will compile

We do this by putting a throws FileNotFoundException on the declaration of main() (or whatever method we're in)

public static void main(String[] args) throws FileNotFoundException{

Scanner in = new Scanner(new File("input.txt"));

Page 12: Week 14 - Monday.  What did we talk about last time?  Inheritance

Writing

Java loves objects If you want to write to a file, you've got

to create a PrintWriter object, based on a FileOutputStream object (which takes the file name as a parameter)

Once you've got a PrintWriter, you can use it just like System.out

PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt"));

Page 13: Week 14 - Monday.  What did we talk about last time?  Inheritance

More exceptions!

Just like making a Scanner from a File, making a PrintWriter from a FileOutputStream will potentially throw a FileNotFoundException

Weird, isn't it? I mean, you don't expect to find a file when you're about to write one

Sometimes Java doesn't make sense Anyway, adding the throws FileNotFoundException to the method declaration will still solve the problem

Page 14: Week 14 - Monday.  What did we talk about last time?  Inheritance

Shut 'em down!

Unlike the command line, you should really close files when you're done reading from them

If you forget, it's okay: Java will automatically close them when your program quits

But, for situations where you're accessing multiple files, it may be important to close them

Scanner in = new Scanner(new File("input.txt"));PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt"));

//do stuffin.close();out.close();

Page 15: Week 14 - Monday.  What did we talk about last time?  Inheritance

Reading and writing example

Let's write a program that prompts the user for 1o int values and then writes them to a file called numbers.txt

Then, let's write another program that opens numbers.txt, reads all 10 numbers, sorts them, and prints them out in order

Page 16: Week 14 - Monday.  What did we talk about last time?  Inheritance

More file practice

Let's write a program that prints the first million prime numbers to a file

Page 17: Week 14 - Monday.  What did we talk about last time?  Inheritance

Quiz

Page 18: Week 14 - Monday.  What did we talk about last time?  Inheritance

Upcoming

Page 19: Week 14 - Monday.  What did we talk about last time?  Inheritance

Next time…

Finish file I/O Lab 14

Page 20: Week 14 - Monday.  What did we talk about last time?  Inheritance

Reminders

Keep working on Project 5