ch11 - Alice in Action with Java Chapter 11 Files and...

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

View Full Document Right Arrow Icon
Alice in Action with Java Chapter 11 Files and Exceptions
Background image of page 1

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

View Full DocumentRight Arrow Icon
Alice in Action with Java 2 Objectives Open and close text files Read values from, and write values to, text files Catch, handle, and throw exceptions
Background image of page 2
Alice in Action with Java 3 Files and Exceptions Main topics of this chapter Reading values into a program from a file Writing values into a file from a program Java’s mechanism for handling exceptions
Background image of page 3

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

View Full DocumentRight Arrow Icon
Alice in Action with Java 4 Introductory Example: Monitoring Acid Rain Revisited Reviewing the acid rain problem PH class models pH values and operations PhLabeler labels samples acidic, neutral, or alkaline New problem Read the pH values from a data file Write the results to another data file Classes used to implement the enhanced program File , PrintWriter , and Exception Input file: a file from which a program reads Output file: a file to which a program writes
Background image of page 4
Alice in Action with Java 5 Introductory Example: Monitoring Acid Rain Revisited (continued)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Alice in Action with Java 6 Introductory Example: Monitoring Acid Rain Revisited (continued)
Background image of page 6
Alice in Action with Java 7 Introductory Example: Monitoring Acid Rain Revisited (continued)
Background image of page 7

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

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

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

View Full DocumentRight Arrow Icon
Alice in Action with Java 10 Opening a File (continued)
Background image of page 10
Alice in Action with Java 11 Opening a File (continued)
Background image of page 11

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

View Full DocumentRight Arrow Icon
Alice in Action with Java 12 Opening a File (continued) Writing to a file is slightly different from reading File object is created from the File class If the file does not exist, it is created File object is wrapped in a PrintWriter object PrintWriter provides write operations How to open data file outside of folder with class file Pass file’s full pathname to original File constructor Use an overloaded File constructor Pass the pathname and filename to the constructor Ex: File inF = new File(pathName,inFName);
Background image of page 12
Alice in Action with Java 13 Opening a File (continued)
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

ch11 - Alice in Action with Java Chapter 11 Files and...

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

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