Title: CS 303E Lecture 25: Files and Streams
1CS 303ELecture 25Files and Streams
Dr. Egon Spengler Don't cross the streams. Dr
Peter Venkman Why not? Dr. Egon Spengler It
would be bad. Dr. Peter Venkman I'm fuzzy on the
whole good/bad thing. what do you mean by
"bad"? Dr. Egon Spengler Try to imagine all life
as you know it stopping instantaneously and
every molecule in your body exploding at the
speed of light. Dr. Peter Venkman That's bad.
Okay. Alright, important safety tip. Thanks
Egon.
2Files
- A file is
- 1. The basic structure for data stored on
secondary storage, such as disk, diskette, tape,
CD, etc. - Permanent data survives indefinitely.
- 2. A software object to model the transfer of
data between the secondary storage and memory.
3Input and Output (I/O)
- Input (reading) The process of transferring
data from a device (e.g., keyboard) or an
existing file to memory. - Output (writing) The reverse from memory to a
device (e.g., screen) or new file. - I/O is often done sequentially from the
beginning of a file to end. - Can also be done randomly jumping around.
- cassette tape vs. cd. Benefits of either?
4Streams
- Files may contain much more data than the program
can conveniently process at once. - A stream (object) is a flow of data between a
program and an external device (e.g., keyboard or
screen) or secondary storage. - A stream typically transfers one data element at
a time. What is an element??
5I/O through Streams
Input
Disk
Input Stream
Processing
Output
Disk
Output Stream
Processing
What is processing??
6File Output
- General process (pseudocode)
- Open an output connection to a file, creating an
output stream - Process data to be written
- Write data to the stream
- . . .
- Close the stream (the output connection to the
file). - Many variations depending on what you want to
output.
7File Input
- General process (pseudocode)
- Open an input connection to a file, creating an
input stream - Read the next piece of data from the stream
- Process it
- . . .
- Close the stream (the input connection to the
file). - Many variations depending on what you want to
input.
8A Portion of Javas File Classes
Object
InputStream
OutputStream
File
FileInputStream
FileOutputStream
ObjectInputStream
ObjectOutputStream
DataInputStream
DataOutputStream
PrintStream
Numerous Streams with minor, but important
differences.
9A Portion of Javas File Classes
Object
StreamTokenizer
Reader
Writer
InputStreamReader
PrintWriter
BufferedReader
FileReader
Used to read data from and write it to Streams.
10Text vs. Binary Files
- Text files contain data encoded in characters.
- E.g., Java source files.
- Can be created, viewed, or edited with a text
editor. - Binary files contain data in the same form as it
appears in the machines memory. - E.g., executable files.
- I/O is handled differently for each.
11Exceptions
- When Java detects an error at run time, an
exception is thrown. - For example, when a program attempts to divide by
zero, Java throws an ArithmeticException.
12try-catch Format
try code that might throw an
exception catch (exception_type_1 e) code
to handle exception of type 1 . . catch
(exception_type_n e) code to handle exception
of type n
13Example A Generic Exception
int a 10, 0, 30 try System.out.println(
a0 / a1) catch (Exception e)
System.out.println ("Error\n"
e.toString()) Exceptions may be checked or
unchecked. Divide by zero, null pointer
exception, and array out of bounds exceptions are
unchecked. The compiler does not expect you to
do anything about these exceptions, because you
should prevent them from ever happening in the
first place. Checked exceptions must be caught.
14Files and Exceptions
- Java requires the programmer to catch file
exceptions. - For example, an IOException can be thrown during
at attempt to open a file. - A FileNotFoundException may occur
-
15Reading from a Text File
- Class InputStreamReader -- one character at a
time. - Class BufferedReader -- one line at a time.
- Class StreamTokenizer -- one word at a time, like
StringTokenizer. - Words are separated or delimited by white space
characters. - The regular white space characters are space,
newline, tab, and carriage return. - Other white space characters may be added
16Ways to Read Text
- InputStreamReader -- character by character
(chars, as ints). - BufferedReader -- line by line (Strings).
- StreamTokenizer -- word by word (Strings,
numbers, etc.).
17Reading Characters
- Connect an InputStreamReader to a FileInputStream
. - Use the method read() to read a character
- Returns -1 if at end of file
- Returns the characters Unicode value (an int)
otherwise.
Disk
FileInputStream
InputStreamReader
18Read Characters and Convert to Uppercase
try FileInputStream stream new
FileInputStream ("myfile") InputStreamReader
reader new InputStreamReader (stream)
output.setText ("") // TextArea int data
reader.read() while (data ! -1) char
ch (char) data ch Character.toUpperCase
(ch) output.append (ch "") data
reader.read() catch (IOException e)
messageBox ("Error in file input\n"
e.toString())