02-June-05 PowerPointPresentation

Download Report

Transcript 02-June-05 PowerPointPresentation

CIS3931 - Intro to JAVA
Lecture Notes Set 6
2-June-05
IO Streams
• IO = Input / Output
• The core JAVA language doesn’t have any
IO methods
– Must import java.io or other io package
• Stream = connection between program
and data source
IO Streams
• Input stream – handles data flowing into a
program
• Output stream – handles data flowing out
of a program
• IO Steams can connect two programs
– Pipe : connects two executing programs
IO Devices
Object
Source? Destination?
Disk file
Both
Running program
Both
Monitor
Destination
Keyboard
Source
Internet
connection
Image scanner
Both
Source
Mouse
Source
Processing Streams
• Processing stream – operates on the data
supplied by another stream. Often acts as
a buffer for the data coming from another
stream.
• Buffer – block of main memory used as a
work area.
Processing Stream - Example
BufferedReader stdin = new
BufferedReader(new InputStreamReader
(System.in));
Readers
• Reader – Abstract class for which all character-oriented
input streams are derived.
Writers
• Writer is an abstract class from which all
character-oriented streams are derived.
InputStream
• Abstract class for which all byte-oriented
input streams are derived.
Output Stream
• Abstract class from which all byte-oriented
output streams are derived.
Writing Text Files
• IO streams are either character-oriented or
byte-oriented.
– Character-oriented IO is specialized for
handling character data.
– Byte-oriented IO is general purpose IO that
involves all types of data.
• We will be discussing character-oriented
output to a disk file and using a Writer
stream.
FileWriter
• Used for character output to a disk file
• FileWriter is a kind of OutputStreamWriter
• OutputSteamWriter is a kind of Writer
• Writer is a kind of Object
FileWriter - Inheritance
http://java.sun.com/j2se/1.5.0/docs/api/index.html
Writing to a file
• Import java.io.*
• Create a FileWriter
– FileWriter writer = new FileWriter(filename);
• Look in the API to see how to print out
using the FileWriter
• Close the FileWriter
Writing to a file
• See FileWriting.java
The importance of close()
• Computer terms often come from business
terms …
• Closing a file means to gather everything
that should go into it and file it away.
• If a file is not closed, the program might
end before the operating system has
finished writing to the data file.
• No close = possible loss of data
• Once a file is closed, you can’t write to it.
FileWriter Contructors
• See the API …
• FileWriter(String fileName, boolean
append)
FileWriter IOExceptions
• Useful when using the append option
• Append expects filename to already exist
• If the program can’t find the filename, it will
throw an exception
• Exception should be handled so that the
program will not crash.
• The constructor, the write() method, and
the close() method call all throw an
exception.
Example
• See FileWriter2.java
Getting the filename from user
input
•
•
•
•
Create null contructor
Create buffered reader
Read filename into string
Try to open filename
• Example : see FileWriter3.java
BufferedWriter
• Allows for more efficient disk input/output
• Useful in programs that do extensive IO.
• Example :
BufferedWriter out = new BufferedWriter(new
FileWriter("stuff.txt"));
PrintWriter
• Used to deal with end-of-line programs
and other frustrations of file output.
• Uses println() method for outputting line of
text with newline at the end.
• Often connected to BufferedWriter (which
is connected to a FileWriter)
• PrintWriter’s methods do not throw
exceptions
Example
• See FileWriter4.java
• Difference is that PrintWriter allows txt files
to show up correctly in Windows programs
such as Notepad
Reading
• Use BufferedReader
• Example :
BufferedReader stdin = new BufferedReader(new
InputStreamReader( System.in ));
• The above example created a
BufferedReader and connects it to the
standard input (not a file).
FileReader
• Used for input of character data from a
disk file.
• Automatically translates the characters
from the disk file format to the internal char
format.
Example
• See FileReader.java
close() with input files
• Not as important as with output files
• Helps to let the operating system manage
resources more efficiently
• File must be closed for writing before it is
opened for reading!
File Input Techniques
• Counting loop – Increment a counter after
each input line is read
• Sentinel-controlled loop – Read in lines
until reaching a line that contains a special
value.
• Result-controlled loop – Read in lines until
a desired result has been achieved.
Counting Loops - Example
• Write a program to add up all the integers
in a file except the first integer which says
how many integers follow
4
23
53
64
91
Counting Loops - Example
• See AddUpAll.java
Sentinel Controlled Input Loop
• Special input value indicates there is no
more data.
• Example : Average all the integers in a file.
-1 = no more integers to average.
78
82
91
-1
Sentinel Controlled Look - Example
• See AddUpAllSentinel.java
• Questions?