Class 24 - Files and Exceptions 1 Goals for Today n n n n...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Files and Exceptions 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Goals for Today n Open and close text files n Read values from, and write values to, text files n Catch, handle, and throw exceptions n Introduce JOptionPane 2 MGMT 29000
Background image of page 2
Introductory Example: Monitoring Acid Rain Revisited n Reviewing the acid rain problem ¨ PH class models pH values and operations ¨ PhLabeler labels samples acidic, neutral, or alkaline n New problem ¨ Read the pH values from a data file ¨ Write the results to another data file n Classes used to implement the enhanced program ¨ File , PrintWriter , and Exception n Input file: a file from which a program reads n Output file: a file to which a program writes 3 MGMT 29000
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Introductory Example: Monitoring Acid Rain Revisited (continued) MGMT 29000 4
Background image of page 4
MGMT 29000 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Files n Some main memory devices: RAM, registers, cache n Some secondary memory devices: hard drive, CD n Programs run in main memory n Programs interact with data in secondary memory ¨ Data is stored in files located within a file system n How a program interacts with data files ¨ Opens a file (for reading or writing) ¨ Reads information from (or writes information to) the file n A loop is usually used to move through the data ¨ Closes the file MGMT 29000 6
Background image of page 6
Opening a File n Prepares a file for reading or writing n The File class connects a program to a file ¨ Ex: File inFile = new File(inFileName); n Note: File does not provide read or write methods n A Scanner can be used to wrap a File object ¨ Purpose: provide program with file read operations ¨ Ex: Scanner fin = new Scanner(inFile); n Read messages can now be sent to fin n Scanner object uses buffer to improve performance ¨ Buffer: data structure for temporary storage MGMT 29000 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Opening a File (continued) MGMT 29000 8
Background image of page 8
Opening a File (continued) MGMT 29000 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Opening a File (continued) n Writing to a file is slightly different from reading ¨ File object is created from the File class
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

Class 24 - Files and Exceptions 1 Goals for Today n n n n...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online