Download presentation
Presentation is loading. Please wait.
Published byRobyn Neal Modified over 9 years ago
1
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling – Process exceptions from program components – Handle exceptions in a uniform manner in large projects – Remove error-handling code from “main line” of execution
2
1 Exceptions A method detects an error and throws an exception – Exception handler processes the error – Uncaught exceptions yield adverse effects Might terminate program execution
3
1 Try-Catch Code that could generate errors put in try blocks – Code for error handling enclosed in a catch clause – The finally clause always executes Termination model of exception handling – The block in which the exception occurs expires throws clause specifies exceptions method throws
4
1 Files Long-term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices – Magnetic disks – Optical disks – Magnetic tapes Sequential and random access files
5
1 Data Hierarchy Smallest data item in a computer is a bit – Bit can be either 0 or 1 – Bit short for “binary digit” Programmers work with higher level data items – Decimal digits: (0-9) – Letters: (A-Z and a-z) – Special symbols: (e.g., $, @, %, &, *, (, ), -, +, “, :, ?, /, etc.) – Java uses Unicode characters composed of 2 bytes A byte is 8 bits long Fields (Java instance variables) – Composed of characters or bytes – Conveys meaning
6
1 Data Hierarchy Data hierarchy – Data items in a computer form a hierarchy Progresses from bits, to characters, to fields, etc. Records – Composed of several fields – Implemented as a class in Java – See Fig. 17.1 for example File is a group of related records – One field in each record is a record key Record key is a unique identifier for a record – Sequential file Records stored in order by record key
7
1 Data Hierarchy RandyRed 1 01001010 J u d y Green SallyBlack TomBlue JudyGreen IrisOrange File Record Field Byte (ASCII character J) Bit
8
1 Files and Streams Java views a file as a stream of bytes (Fig. 17.2) – File ends with end-of-file marker or a specific byte number – File as a stream of bytes associated with an object Java also associates streams with devices – System.in, System.out, and System.err – Streams can be redirected
9
1 Files and Streams File processing with classes in package java.io – FileInputStream for byte-based input from a file – FileOutputStream for byte-based output to a file – FileReader for character-based input from a file – FileWriter for character-based output to a file
10
1 Files and Streams Java views a file as a stream of bytes (Fig. 17.2) – File ends with end-of-file marker or a specific byte number – File as a stream of bytes associated with an object Java also associates streams with devices – System.in, System.out, and System.err – Streams can be redirected
11
1 Files and Streams File processing with classes in package java.io – FileInputStream for byte-based input from a file – FileOutputStream for byte-based output to a file – FileReader for character-based input from a file – FileWriter for character-based output to a file
12
1 Java’s view of a file of n bytes 03... 124589 n-1 end-of-file marker 67
13
1 Files and Streams Buffering – Improves performance of I/O – Copies each output to a region of memory called a buffer – Entire buffer output to disk at once One long disk access takes less time than many smaller ones – BufferedOutputStream buffers file output – BufferedInputStream buffers file input
14
1 Class File – Provides useful information about a file or directory – Does not open files or process files Fig. 17.3 lists some useful File methods
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.