File Processing Midterm Logistics Midterm tonight , 7PM 9PM Last - - PowerPoint PPT Presentation

file processing
SMART_READER_LITE
LIVE PREVIEW

File Processing Midterm Logistics Midterm tonight , 7PM 9PM Last - - PowerPoint PPT Presentation

File Processing Midterm Logistics Midterm tonight , 7PM 9PM Last name A O: Cemex Auditorium (Knight Management Center) Last name P Z: Braun Auditorium (Mudd Chemistry Building) Welcome to Big Data Getting Data Into


slide-1
SLIDE 1

File Processing

slide-2
SLIDE 2
slide-3
SLIDE 3

Midterm Logistics

  • Midterm tonight, 7PM – 9PM
  • Last name A – O: Cemex Auditorium
  • (Knight Management Center)
  • Last name P – Z: Braun Auditorium
  • (Mudd Chemistry Building)
slide-4
SLIDE 4

Welcome to Big Data

slide-5
SLIDE 5

Getting Data Into Programs

  • Put it directly in the program:
  • Define constants holding your values.
  • Get it from the user:
  • Mouse events, readLine, etc.
  • Generate it randomly:
  • Use a RandomGenerator.
  • Get it from an external source.
  • Store it in a file and read it later.
slide-6
SLIDE 6

Reading Files

  • Virtually all programs that you've used at

some point read files from disk:

  • Word processing (documents)
  • Eclipse (Java files)
  • Web browser (cookies)
  • IM client (stored login information)
  • Games (saved progress)
  • Music player (songs)
slide-7
SLIDE 7

The Structure of Files

  • A file is just a series of bits (ones and

zeros).

  • Those bits can have structure:
  • Plain-text: Bits represent characters.
  • JPEG: Bits encode information about the

structure of an image.

  • MP3: Bits encode frequency information

about music.

  • etc.
slide-8
SLIDE 8

The Structure of Files

A file is just a series of bits (ones and zeros). Those bits can have structure:

  • Plain-text: Bits represent characters.

JPEG: Bits encode information about the structure of an image. MP3: Bits encode frequency information about music. etc.

slide-9
SLIDE 9

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
slide-10
SLIDE 10

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"

Step one: Open the file for reading.

slide-11
SLIDE 11

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt"));
slide-12
SLIDE 12

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt"));

To use the BufferedReader and FileReader types, you need to import java.io.*;

slide-13
SLIDE 13

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt"));
slide-14
SLIDE 14

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt"));
slide-15
SLIDE 15

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt"));

Step Two: Read the file,

  • ne line at a time.
slide-16
SLIDE 16

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine();
slide-17
SLIDE 17

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine();
slide-18
SLIDE 18

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair,
slide-19
SLIDE 19

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair,
slide-20
SLIDE 20

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine();
slide-21
SLIDE 21

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine();
slide-22
SLIDE 22

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there
slide-23
SLIDE 23

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there
slide-24
SLIDE 24

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine();
slide-25
SLIDE 25

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine();
slide-26
SLIDE 26

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today
slide-27
SLIDE 27

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today
slide-28
SLIDE 28

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine();
slide-29
SLIDE 29

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine();
slide-30
SLIDE 30

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away...
slide-31
SLIDE 31

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away...
slide-32
SLIDE 32

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine();
slide-33
SLIDE 33

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine();
slide-34
SLIDE 34

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish"
slide-35
SLIDE 35

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish"
slide-36
SLIDE 36

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine();
slide-37
SLIDE 37

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null*
slide-38
SLIDE 38

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null*

Step Three: Close the file.

slide-39
SLIDE 39

Yesterday, upon the stair, I met a man who wasn’t there He wasn’t there again today I wish, I wish he’d go away...

  • Hugh Mearns, "Antagonish"
BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close();
slide-40
SLIDE 40

Let's Try It Out!

slide-41
SLIDE 41

There's a catch...

slide-42
SLIDE 42

Sometimes Things Break

  • Programs sometimes encounter

unexpected errors.

  • Sometimes these are bugs:
  • Dividing by zero.
  • Sending a message to a null object.
  • Sometimes these are due to external

factors:

  • Network errors.
  • Missing files.
slide-43
SLIDE 43

Exceptional Cases

  • If Java encounters a case where it can't

proceed as normal, it will cause an exception.

  • Java requires that your program handle

certain types of exceptions.

  • Think of exceptions as rerouting control in

an emergency:

  • If all goes well, program continues as usual.
  • If something goes wrong, handle the

emergency.

slide-44
SLIDE 44

Let's Try It Out!

slide-45
SLIDE 45

Let's try It Out!

slide-46
SLIDE 46

try-ing Your Best

  • To use a method or class that might cause an

exception, you need to tell Java to try its best, knowing that it might fail.

slide-47
SLIDE 47

try-ing Your Best

  • To use a method or class that might cause an

exception, you need to tell Java to try its best, knowing that it might fail.

BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close();
slide-48
SLIDE 48

try-ing Your Best

  • To use a method or class that might cause an

exception, you need to tell Java to try its best, knowing that it might fail.

try { BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close(); }
slide-49
SLIDE 49

There's a catch...

slide-50
SLIDE 50

There's a catch...

slide-51
SLIDE 51

try and catch me

  • If an exception occurs, you may need to tell

Java to catch that exception.

slide-52
SLIDE 52

try and catch me

  • If an exception occurs, you may need to tell

Java to catch that exception.

try { BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close(); }
slide-53
SLIDE 53

try and catch me

  • If an exception occurs, you may need to tell

Java to catch that exception.

try { BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close(); } catch (IOException e) { println("An error occurred: " + e); }
slide-54
SLIDE 54

try and catch me

  • If an exception occurs, you may need to tell

Java to catch that exception.

try { BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close(); } catch (IOException e) { println("An error occurred: " + e); } If something fails up here…
slide-55
SLIDE 55

try and catch me

  • If an exception occurs, you may need to tell

Java to catch that exception.

try { BufferedReader br = new BufferedReader(new FileReader("poem.txt")); String line1 = br.readLine(); // Yesterday, upon the stair, String line2 = br.readLine(); // I met a man who wasn't there String line3 = br.readLine(); // He wasn't there again today String line4 = br.readLine(); // I wish, I wish he'd go away... String line5 = br.readLine(); // - Hugh Mearns, "Antagonish" String line6 = br.readLine(); // *Returns null* br.close(); } catch (IOException e) { println("An error occurred: " + e); } If something fails up here… … we immediately jump down here.
slide-56
SLIDE 56

Finally... let's make this program work!

slide-57
SLIDE 57

Reading a File

  • The idiomatic “read all the lines of a file” code is shown here:
try { BufferedReader br = /* … open the file … */ while (true) { String line = br.readLine(); if (line == null) break; /* … process line … */ } br.close(); } catch (IOException e) { /* … handle error … */ }
slide-58
SLIDE 58

Writing to Files

  • Writing to files is conceptually similar to

reading files.

  • Open the file for writing (using PrintWriter

instead of BufferedReader).

  • Write the contents you'd like to the file.
  • Close the file.
slide-59
SLIDE 59

/ / / / / / DANGER \ \ \ \ \

Writing to an existing file will permanently

  • verwrite the contents of that file. Don't write

to a file unless you're okay setting it on fire!

slide-60
SLIDE 60

Putting Everything Together

slide-61
SLIDE 61 http://2.bp.blogspot.com/_XqLF69h4gXA/TLVwfxDMXnI/AAAAAAAAAZU/9lWr-2sMzmc/s1600/empty+fridge.JPG
slide-62
SLIDE 62

Big

Ascent Descent
slide-63
SLIDE 63

Big

Ascent Descent

(x, y - ascent)

slide-64
SLIDE 64

Linking Objects Together

  • The GCompound class holds a collection of GObjects

that are treated as a unit.

  • You can add objects to a GCompound by using the

GCompound's add method.

(0, 0)
slide-65
SLIDE 65

GCompound

(0, 0)

Big

(0, ascent) (0, 0)
slide-66
SLIDE 66

GCompound

(0, 0)

Big

(0, ascent) (0, 0) (width, height)
slide-67
SLIDE 67

Method Overrides

  • A subclass can override a method of a

superclass by providing its own implementation.

  • When a method is invoked, Java will find

the most specific version of that method to invoke.