84
File Reading CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 15 With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.

File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

File Reading

CS106A, Summer 2019Sarai Gould && Laura Cruz-Albrecht

Lecture 15

With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.

Page 2: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Midterm: check out website page

○ Download Bluebook, and be sure to have 2 factor authentication with passcodes; see Tuesday’s lecture for helpful links.

● Midterm review session: tomorrow 10:30AM in Gates B01

2

Page 3: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!

3

Page 4: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

4

Page 5: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

○ One late day is 24 hours.

5

Page 6: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

○ One late day is 24 hours.○ You have 3 late days for the whole quarter.

6

Page 7: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

○ One late day is 24 hours.○ You have 3 late days for the whole quarter.○ You can only use up to 2 late days on any assignment.

7

Page 8: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

○ One late day is 24 hours.○ You have 3 late days for the whole quarter.○ You can only use up to 2 late days on any assignment.○ We will not accept assignments more than 2 days late. They will not

be graded and will receive a zero.

8

Page 9: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

○ One late day is 24 hours.○ You have 3 late days for the whole quarter.○ You can only use up to 2 late days on any assignment.○ We will not accept assignments more than 2 days late. They will not

be graded and will receive a zero.○ If you turn in an assignment late, but are out of late days, you will

lose a bucket grade for style and functionality.

9

Page 10: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Announcements● Assignment 3 was due at 10am!● Assignment 4 will be out after lecture!● Reminders about Due Dates and Late Days:

○ One late day is 24 hours.○ You have 3 late days for the whole quarter.○ You can only use up to 2 late days on any assignment.○ We will not accept assignments more than 2 days late. They will not

be graded and will receive a zero.○ If you turn in an assignment late, but are out of late days, you will

lose a bucket grade for style and functionality.○ If your partner has late days and you don’t, your personal grade

will still go down if you both turn in the assignment late. 10

Page 11: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Plan for Today

● Review: Characters and Strings● Reading Information from a File● Reading Lines from a File● Reading Strings from a File● Practice: Let’s Help Duke!

11

Page 12: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Review: Char Loops

12

// prints the characters a to z

for (char ch = 'a'; ch <= 'z'; ch++) {

println(ch);

}

Page 13: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Review: Looping over Strings

A common String programming pattern is looping over a String and operating on each character.

for (int i = 0; i < str.length(); i++) {

char ch = str.charAt(i);

// do something with ch here

}

13

Page 14: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Review: Looping over StringsAnother common String programming pattern is building up a new string by adding characters to it over time.

// Creates a new String containing digits 0 through 4

String str = "";

for (int i = 0; i < 5; i++) {

str += i;

}

println(str); // 01234

14

Page 15: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Another Question

15

Uhhh, Sarai? I have a question.

Page 16: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Another Question

16

I got a high score in my breakout game, and I really wanted to save it, but when I closed my program, I lost my score! 😭How can I save my high score?

Page 17: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

How Can We Save Information?

17

Page 18: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

How Can We Save Information?

In a Database

In a File

In our Mind?

18

Page 19: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

How Can We Save Information?

In a Database

In a File

In our Mind?

19

Page 20: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Almost Every Program Uses Files!

Almost every program you use reads files from disk:

● Word processing (documents)● Web browser (cookies)● Games (saved progress)● Eclipse (Java files)● Music player (songs)● And so many more!

20

Cookies, yum… 🍪

Page 21: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Files are basically just a series of bits (zeros and ones).

Those bits can have different structures.

● Plain-Text: Bits represent characters.● JPEG: Bits encode information about the structure of an

image.● MP3: Bits encode frequency information about music, etc.

What are Files?

21

Sounds cool.

Page 22: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Files are basically just a series of bits (zeros and ones).

Those bits can have different structures.

● Plain-Text: Bits represent characters.● JPEG: Bits encode information about the structure of an

image.● MP3: Bits encode frequency information about music, etc.

What are Files?

22

Page 23: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Reading in a File

23

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Page 24: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

First, Let’s Open the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

24

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Opening a File:

Page 25: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Importing Libraries

import java.util.*; // for Scanner

import java.io.*; // for file

...

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

25

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Ooops, missing something!

Page 26: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

26

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Now, let’s read in a line!

Page 27: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// “Are you there Diary? It’s me, Duke.”

String line1 = input.nextLine();

27

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Now, let’s read in a line!

input is here

Page 28: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// “Are you there Diary? It’s me, Duke.”

String line1 = input.nextLine();

28

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

The Scanner moves to the next line...

input is here

Page 29: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// “Are you there Diary? It’s me, Duke.”

String line1 = input.nextLine();

// “”

String line2 = input.nextLine();

29

Are you there Diary? It’s me, Duke.

...

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 30: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// “Are you there Diary? It’s me, Duke.”

String line1 = input.nextLine();

// “”

String line2 = input.nextLine();

30

Are you there Diary? It’s me, Duke.

...

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 31: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “I had a really tough day yesterday. Between stack”

String line3 = input.nextLine();

31

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 32: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “I had a really tough day yesterday. Between stack”

String line3 = input.nextLine();

32

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 33: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “I had a really tough day yesterday. Between stack”

String line3 = input.nextLine();

// “overflows and integer division, it felt like I got”

String line4 = input.nextLine(); 33

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 34: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “I had a really tough day yesterday. Between stack”

String line3 = input.nextLine();

// “overflows and integer division, it felt like I got”

String line4 = input.nextLine(); 34

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 35: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Now, Let’s Read the File!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “nothing done! Hopefully today will be a better day!”

String line5 = input.nextLine();

35

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

We can continue reading Duke’s Diary!

input is here

Page 36: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

What Should We Do Next?

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “nothing done! Hopefully today will be a better day!”

String line5 = input.nextLine();

36

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

What would you do if you just read someone’s diary?

Page 37: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Closing the Scanner!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

...

// “nothing done! Hopefully today will be a better day!”

String line5 = input.nextLine();

input.close();37

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Close the diary!

Page 38: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Simplifying Our Code

38

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Reading each line one at a time became tedious! And what if we don’t know how many lines are in the Diary*?!?

Let’s see if we can simplify our strategy.

*Errrr, I mean file

Page 39: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Reading One Line at a Time

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

while(input.hasNextLine()){

// What should we do in here?

}

39

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Let’s use hasNextLine()!

Page 40: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Reading One Line at a Time

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// This will print out every line in the file!

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

40

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

As long as there is another line, let’s print it!

Page 41: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Missing Something Again!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// This will print out every line in the file!

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

41

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Wait, what are we missing?

Page 42: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Always Close the Scanner!

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

// This will print out every line in the file!

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

input.close();42

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

Make sure to close the Diary* when you’re done with it!

*Definitely meant Scanner

Page 43: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Let’s Code It!

43

Page 44: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

Scanner input = new Scanner(new File(“Dukes Diary.txt”));

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open Duke’s Diary!”);

} 44

Are you there Diary? It’s me, Duke.

I had a really tough day yesterday. Between stack

overflows and integer division, it felt like I got

nothing done! Hopefully today will be a better day!

If we can’t open the file, we need to catch the error.

Page 45: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

...

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

45

We will try to open the file, but if we can’t it will throw a FileNotFoundException. We need to catch that error and print out a message.

Page 46: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

...

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

46

We try to open the file...

Page 47: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

...

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

47

It’s not very effective… I CANT OPEN THAT FILE WHY ARE YOU MAKING ME OPEN THE FILE I CAN’T OPEN IT WHAT WERE YOU

More like FileNotFoundTemperTantrum...

Page 48: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

...

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

48

It’s not very effective…

A FileNotFoundException is thrown.

Page 49: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

...

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

49

This mean we skip all of the code that would have happened after we opened the file. We also won’t close the Scanner because we couldn’t open the file in the first place!

Page 50: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

...

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

50

It’s not very effective…

A FileNotFoundException is thrown.

Page 51: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

There’s a Catch

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

input.close();

} catch (FileNotFoundException ex){

println(“Couldn’t open this file!”);

}

51

A FileNotFoundException is thrown.

Our program prints: “Couldn’t open this file!”

Page 52: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

A More General Version

try {

// trying to open file!

Scanner input = new Scanner(new File(“filenameToOpen.txt”));

input.close();

} catch (IOException ex){

println(“The error is: ” + ex);

}

52

An IOException is thrown.

Our program prints: “The error is: [type of error]”

Page 53: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Try-Catch

53

If you want to open a file, you must have a try-catch.

Your program needs a guarantee that you will do something if you are unable to open your file!

Page 54: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

A Few Other Exceptions

54

NoSuchElementException

- You read past the end of input

InputMismatchException

- You read the wrong type of token (“hi” as an int)

Page 55: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

What About One Word at a Time?

55

Sometimes, we might want to read data a little bit differently.

Instead of a line at a time, we might want to read in smaller pieces of data!

Page 56: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

56

123456 password qwerty iloveCS106A letmein

It’s hard to see, but this is a file containing Duke’s passwords!

Page 57: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

57

123456 password qwerty iloveCS106A letmein

What should we do first?

Page 58: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

58

123456 password qwerty iloveCS106A letmein

try {

Scanner input = new Scanner(new File(“Dukes Passwords.txt”));

} catch (IOException ex){

println(“The error is: ” + ex);

}

Open the file! … with a try-catch.

Page 59: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

59

123456 password qwerty iloveCS106A letmein

try {

Scanner input = new Scanner(new File(“Dukes Passwords.txt”));

} catch (IOException ex){

println(“The error is: ” + ex);

}

Let’s read through the file… but how?

Page 60: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

60

123456 password qwerty iloveCS106A letmein

try {

Scanner input = new Scanner(new File(“Dukes Passwords.txt”));

String password1 = input.next();

// “123456”

println(password1);

} catch (IOException ex){

println(“The error is: ” + ex);

}

.next() will read in a one word String at a time.

Page 61: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

61

123456 password qwerty iloveCS106A letmein

try {

Scanner input = new Scanner(new File(“Dukes Passwords.txt”));

String password1 = input.next();

// “123456”

println(password1);

String password2 = input.next();

// “password”

println(password2);

} catch (IOException ex){

println(“The error is: ” + ex);

}

.next() will read in a one word String at a time.

Page 62: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

62

123456 password qwerty iloveCS106A letmein

try {

Scanner input = new Scanner(new File(“Dukes Passwords.txt”));

// Will print all passwords in the file!

while(input.hasNext()){

String password = input.next();

println(password);

}

} catch (IOException ex){

println(“The error is: ” + ex);

}

Using .hasNext() will check for more one word Strings!

Page 63: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

One Word at a Time

63

123456 password qwerty iloveCS106A letmein

try {

Scanner input = new Scanner(new File(“Dukes Passwords.txt”));

// Will print all passwords in the file!

while(input.hasNext()){

String password = input.next();

println(password);

}

input.close();

} catch (IOException ex){

println(“The error is: ” + ex);

}

And then we have to close the Scanner for the password file!

Page 64: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Let’s Code It!

64

Page 65: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Different Scanner Methods

65

Page 66: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Can We Combine Scanner Methods?

66

Page 67: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Can We Combine Scanner Methods?

Absolutely! Let’s write a program that will count how many words are in each line of Duke’s Diary entry!

67

Page 68: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Let’s Code It!

68

Page 69: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Reading from a String

69

String text = “Scanners can read from plain Strings too!”

Scanner input = new Scanner(text);

// Will print all of the words in the text!

while(input.hasNext()){

String word = input.next();

println(word);

}

input.close();

A Scanner can read from a String as well!

Page 70: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Choosing Files

70

try {

Scanner input = new Scanner(new File(“myFileName.txt”));

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

input.close();

} catch (IOException ex){

println(“The error is: ” + ex);

}

What if we want to pick the file we open?

Page 71: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Choosing Files

71

// Gets filename from user

String filename = promptUserForFile(“What file would you like to open?”);

try {

Scanner input = new Scanner(new File(filename));

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

input.close();

} catch (IOException ex){

println(“The error is: ” + ex);

}

What if we want to pick the file we open?

Page 72: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Choosing Files

72

// Strategy 2

String filename = promptUserForFile(“What file would you like to open?”);

try {

Scanner input = new Scanner(new File(filename));

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

input.close();

} catch (IOException ex){

println(“The error is: ” + ex);

}

What if we want to pick the file we open?

This automatically prompts a user for a new file if they mess up the filename!

Page 73: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Choosing Files

73

// Strategy 2

String filename = promptUserForFile(“What file would you like to open?”);

try {

Scanner input = new Scanner(new File(filename));

while(input.hasNextLine()){

String line = input.nextLine();

println(line);

}

input.close();

} catch (IOException ex){

println(“The error is: ” + ex);

}

What if we want to pick the file we open?

Note: we changed this to the variable filename.

Page 74: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Secrets...

In some text files in Sarai’s Eclipse, she’s hidden some secrets. Can we find the correct files and print out what’s inside of them?

74

Page 75: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Let’s Code It!

75

Page 76: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

76

Sarai, my high score?

Page 77: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Helping Duke Save High Scores!

77

Note: Right now, we can’t write to a file, be we’ve learned how to extract data from a file. If we assume we’ve already stored Breakout high scores in a file, how can we extract it?

Page 78: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Helping Duke Save High Scores!

78

What’s the Pseudocode for Extracting it?

What Format Should We Save Our Data In?

Page 79: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Helping Duke Save High Scores!

79

What’s the Pseudocode for Extracting it?

What Format Should We Save Our Data In?

Each line will have:

Type: String int double

Data: name bricks seconds

Page 80: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Helping Duke Save High Scores!

80

What’s the Pseudocode for Extracting it?

try to open fileWhile there is a new line:

Extract nameExtract number of bricksExtract number of secondsPrint data

catch any errorsprint error message

What Format Should We Save Our Data In?

Each line will have:

Type: String int double

Data: name bricks seconds

Page 81: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Reminder: Different Scanner Methods

81

Page 82: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Helping Duke Save High Scores!

82

What’s the Pseudocode for Extracting it?

try to open fileWhile there is a new line:

Extract nameExtract number of bricksExtract number of secondsPrint data

catch any errorsprint error message

What Format Should We Save Our Data In?

Each line will have:

Type: String int double

Data: name bricks seconds

Page 83: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Let’s Code It!

83

Page 84: File Reading · 2019-07-18 · Announcements Assignment 3 was due at 10am! Assignment 4 will be out after lecture! Reminders about Due Dates and Late Days: One late day is 24 hours

Plan for Today

● Review: Characters and Strings● Reading Information from a File● Reading Lines from a File● Reading Strings from a File● Practice: Let’s Help Duke!

84