Stream IO - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Stream IO

Description:

... read () throws IOException ... public void close () throws IOException. Close the input stream and release ... (char c, String fileName) throws IOException ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 28
Provided by: Empl154
Category:

less

Transcript and Presenter's Notes

Title: Stream IO


1
Stream I/O
2
Objectives
  • 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.

3
Java Streams
  • A data stream is a sequence of bytes

4
Java I/O library structure
  • Four class hierarchies to support reading and
    writing data streams

5
Abstract 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.

6
Byte 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()

7
Byte 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
8
Reading 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()
9
Wrapper class
  • A wrapper class adds functionality and forwards
    requests for some services to its component.

wraps
10
Wrapper class FilterInputStream
  • The FilterInputStream is a wrapper class
  • DataInputStream reads in primitive Java data
    types from the input stream

wraps
11
Byte 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()
12
Input 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

13
Input character streams InputStreamReader
  • Wraps an InputStream and converts each
    InputStream byte to Unicode character.

14
Input character streams FileReader
  • FileReader extends InputStreamReader.
  • It is an InputStreamReader wrapping a
    FileInputStream.

15
Example 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()
16
Example 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
17
Example 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
18
Reading 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)
19
Example 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()
20
SimpleInput 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
21
Output 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
22
Output 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)
23
Output character streams
public abstract class Writer public
abstract void write (int c) throws IOException
// Write Unicode character
24
PrintWriter
  • 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.
25
Summary
  • 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.

26
Summary
27
Summary
Write a Comment
User Comments (0)
About PowerShow.com