Title: Stream IO
1Stream I/O
2Objectives
- Byte streams and character streams, and the class
structure of the standard libraries for
manipulating these streams - To use the standard libraries to construct a
utility class such as nhUtilities.basicIO.BasicFi
leReader - To use standard Java stream classes to read and
write bytes and characters. - The chapter is technical in nature.
3Java Streams
- A data stream is a sequence of bytes
4Java I/O library structure
- Four class hierarchies to support reading and
writing data streams
5Abstract class InputStream
- Methods for reading a byte stream includes
public abstract int read () throws
IOException Read and return the next byte of data
from the input stream. Return -1 if the end of
the stream is encountered. ensure (0 lt
this.read() this.read() lt 255) this.read()
-1 public void close () throws
IOException Close the input stream and release
associated resources.
- Client method invoking read/close must either
catch IOexception or include a throws clause in
its header.
6Byte Input Stream Standard input
- Input byte stream generally available in every
program. - Default source of bytes is keyboard.
- Accessed by constant in from class
java.lang.System - public static final InputStream in // standard
input
- Read a byte from standard input by writing
- int b System.in.read()
7Byte Input Stream FileInputStream
- A file is the source of bytes. File is opened
when FileInputStream is created. File name is - a String file name,
- a File object,
- or a system-specific file descriptor
public FileInputStream (String name) throws
FileNotFoundException, SecurityException public
FileInputStream (File file) throws
FileNotFoundException, SecurityException public
FileInputStream (FileDescriptor fd) throws
FileNotFoundException, SecurityException
8Reading bytes from a file
public void processFile (String fileName) throws
IOException FileInputStream in new
FileInputStream(fileName) int b while ((b
in.read()) ! -1) process b in.close()
9Wrapper class
- A wrapper class adds functionality and forwards
requests for some services to its component.
wraps
10Wrapper class FilterInputStream
- The FilterInputStream is a wrapper class
- DataInputStream reads in primitive Java data
types from the input stream
wraps
11Byte Input Stream BufferedInputStream
- Uses buffer to store input from the stream. So
the OS need only access occasionally to fill the
buffer.
FileInputStream in new FileInputStream(noise.da
t) BufferedInputStream bf new
BufferedInputStream(in) DataInputStream data
new DataInputStream(bf) int i try
while (true) i data.readInt() process(i)
catch (EOFException e)
data.close()
12Input character streams BufferedReader
- Reader reads a stream of Unicode characters. It
is an abstract class - BufferedReader buffers character stream input.
- Provides a method to read a line of characters
- public String readLine() throws IOException
13Input character streams InputStreamReader
- Wraps an InputStream and converts each
InputStream byte to Unicode character.
14Input character streams FileReader
- FileReader extends InputStreamReader.
- It is an InputStreamReader wrapping a
FileInputStream.
15Example 1
public void displayBinaryFile (String fileName)
throws IOException BufferedInputStream in
new BufferedInputStream( new
FileInputStream(fileName)) int b while ((b
in.read()) ! -1) String hex
Integer.toString(b,16) if (hex.length()
1) hex '0' hex System.out.println(hex)
in.close()
16Example 2
public int charCount (char c, String fileName)
throws IOException FileReader in new
FileReader(fileName) int count 0 int
charRead while ((charRead in.read()) !
-1) if ((char)charRead c) count count
1 in.close() return count
17Example 3
public int charCount (char c, String fileName)
throws IOException BufferedReader in new
BufferedReader( new FileReader(fileName))
int count 0 String line while ((line
in.readLine()) ! null) if (line.charAt(0)
c) count count 1 in.close() return
count
18Reading primitive values SimpleInput
- Construct a set of methods to read
representations of integers and doubles from
standard input.
public class SimpleInput public static int
readInt (String message) public static
double readDouble (String message) public
static char readChar (String message) public
static String readString (String message)
public static void prompt (String message)
19Example of use of SimpleInput
- A client will invoke a method something like
this - int age SimpleInput.readInt("Enter your age
") - The user will see prompt, and key in an integer,
public class SimpleInput private static
BufferedReader in new BufferedReader( new
InputStreamReader(System.in)) private static void
prompt (String message) System.out.print(messa
ge) System.out.flush()
20SimpleInput class implementation
public static int readInt (String message)
throws IOException, NumberFormatException
prompt(message) return Integer.parseInt(in.re
adLine()) public static double
readDouble (String message) throws IOException,
NumberFormatException prompt(message) return
Double.parseDouble(in.readLine())
public static char readChar (String message)
throws IOException prompt(message) return
in.readLine().charAt(0) public
static String readString (String message) throws
IOException prompt(message) return
in.readLine() //end SimpleInput
21Output byte streams
- Mirrors the input classes
wraps
class OutputStream public abstract void write
(int b) throws IOException public void close
() throws IOException public void flush ()
throws IOException
22Output byte streams
class FileOutputStream public
FileOutputStream (String name) throws
FileNotFoundException, SecurityException
public FileOutputStream (String name, boolean
append) throws public FileOutputStream
(File file) throws public
FileOutputStream (File file, boolean append)
throws public FileOutputStream
(FileDescriptor fd) throws class
DataOutputStream public void writeBoolean
(boolean v) public void writeByte (int v)
public void writeChar (int v) public
void writeChars (String s) public void
writeDouble (double v) public void
writeInt (int v)
23Output character streams
public abstract class Writer public
abstract void write (int c) throws IOException
// Write Unicode character
24PrintWriter
- It extends and wraps a Writer
- It provides functionality for writing string
representations of primitive values and objects. - For objects it uses Objects toString method
public PrintWriter (OutputStream out) An
OutputStreamWriter that converts characters to
bytes public PrintWriter (OutputStream
out,boolean autoFlush) If autoFlush is true,
calls its flush method after every invocation of
println. public PrintWriter (Writer out) Create a
PrintWriter that sends output to the specified
Writer. public PrintWriter (Writer out, boolean
autoFlush) Creates PrintWriter that outputs to
specified Writer.
25Summary
- Four root classes, one for each category of
stream -
- Character streams in Java represent characters
with a 16-bit Unicode encoding. Any other stream
is considered a byte stream.
26Summary
27Summary