InputOutput - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

InputOutput

Description:

skipBytes(int) Skips exactly n bytes of input in the underlying input stream. ... public class Cat extends Animal implements Serializable //contents of the Cat class. ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 18
Provided by: gae5
Category:

less

Transcript and Presenter's Notes

Title: InputOutput


1
Input/Output
  • Streams
  • and
  • Files

2
Streams
  • An abstract representation of the flow of data
    from an input device or to an output device.
  • Devices
  • the keyboard (standard in) System.in
  • the screen (standard out) System.out
  • a file on disk
  • You can write to a stream or read from a stream
  • In Java, a stream can be handled as byte stream
    or a character stream. Java Classes for working
    with byte streams are separated from Classes for
    working with character streams in Javas Class
    hierarchy.

3
Byte Stream Data Formats
  • Incoming stream data is not formatted. Bytes
    stream in, one after the other, and the software
    has to figure out how to group bytes into int,
    double, char, float or String groups.
  • Outgoing stream data must also be formatted by
    software. (int - 4 bytes, char - 1 byte, etc)

4
Java Classes for Input/Output
  • InputStream
  • Abstract base class for these classes
  • DataInputSteam
  • FileInputStream
  • OutputStream
  • Abstract base class for these classes
  • DataOutputStream
  • FileOutputStream
  • File
  • Represents a pathname to a file for input or
    output.

5
One thing at a time
  • DataInputStream
  • allows you to read one int, char, double, etc
  • at a time - not Strings of text
  • DataOutputStream
  • allows you to write one int, char, double, etc
  • at a time - not Strings of text

6
DataInputStream class
  • read(byte) Reads up to byte.length bytes of
    data from this data input stream into an array of
    bytes.
  • read(byte, int, int) Reads up to len bytes of
    data from this data input stream into an array of
    bytes.
  • readChar() Reads a Unicode character from this
    data input stream.
  • readDouble() Reads a double from this data input
    stream.
  • readFloat() Reads a float from this data input
    stream.
  • readFully(byte) Reads b.length bytes from this
    data input stream into the byte array.
  • readFully(byte, int, int) Reads exactly len
    bytes from this data input stream into the byte
    array.
  • readInt() Reads a signed 32-bit integer from
    this data input stream.
  • readLine() Reads the next line of text from this
    data input stream. Deprecated ( replaced by
    readUTF() )
  • skipBytes(int) Skips exactly n bytes of input in
    the underlying input stream.

7
DataOutputStream class
  • size() Returns the number of bytes written to
    this data output stream.
  • write(byte, int len, int off) Writes len bytes
    from the specified byte array starting at offset
    off to the underlying output stream.
  • write(int) Writes the specified byte to the
    underlying output stream.
  • writeBoolean(boolean) Writes a boolean to the
    underlying output stream as a 1-byte value.
  • writeChar(int) Writes a char to the underlying
    output stream as a 2-byte value, high byte first.
  • writeChars(String) Writes a string to the
    underlying output stream as a sequence of
    characters.
  • writeDouble(double) Converts the double argument
    to a long using the doubleToLongBits method in
    class Double, and then writes that long value to
    the underlying output stream as an 8-byte
    quantity, high byte first.
  • writeFloat(float) Converts the float argument to
    an int using the floatToIntBits method in class
    Float, and then writes that int value to the
    underlying output stream as a 4-byte quantity,
    high byte first.
  • writeInt(int) Writes an int to the underlying
    output stream as four bytes

8
Character Streams
  • Java interprets character data as Unicode.
  • Unicode(UTF) - a special data format used by java
    to represent text. It uses the ASCII code as a
    base, but extends it to accommodate foreign
    languages, so java programs can be understood
    everywhere!
  • Javas Reader Classes work with Unicode.

9
Reading Text with Readers
  • Readers provide methods for reading character
    data in entire lines of text.
  • attach an InputStreamReader to System.in
  • InputStreamReader inReader new
    InputStreamReader(System.in)
  • wrap the InputStreamReader with a BufferedReader
    for buffering Strings of data
  • BufferedReader input new BufferedReader(inReader
    )
  • read a line of text from System.in
  • name input.readLine()

10
Files
11
Formatting File I/O
  • A file becomes a device when it is associated
    with a FileInputStream or FileOutputStream. You
    can read the file as a stream of bytes or as
    character data by wrapping the file stream with
    the appropriate formatter class.
  • Create a FileInputStream using a real filename
  • FileInputStream infile new FileInputStream(myfi
    le.txt)
  • Wrap it with a DataInputStream so it is easier to
    read from
  • DataInputStream inputFile new
    DataInputStream(infile)

12
Keeping Java Code Portable
  • Pathname separator characters (back slash or
    forward slash) differ between Windows and Unix
  • a\myfile.txt Windows
  • a/myfile.txt Unix
  • File.separator
  • gives you a \ for Windows
  • or a / for Unix
  • Build a file and pathname that is portable
  • String pathname a File.separator
    file.txt

13
Java Inheritance
  • Java allows you to process raw data using base
    class objects
  • DataInputStream, DataOutputStream,
    FileInputStream, FileOutputStream
  • Java also allows you to add formatting
    functionality by chaining data formatting,
    reading and writing classes together.
  • InputStreamReader, OutputStreamWriter,
    BufferedReader, BufferedWriter
  • Each class you add to the chain adds a little
    more functionality.

14
Serializing an Object
  • To save an object, in its current state, to a
    file so you can read it again later.
  • To reduce an object to a byte stream so you can
    send it to another system.
  • For an object to be Serializable it must
  • Have a default constructor, and all of its parent
    classes must have default constructors.
  • Implement the Serializable interface
  • Interfaces are provided by Java to allow for
    something similar to multiple inheritance.

15
Serializable Interface
  • public class Cat extends Animal implements
    Serializable
  • //contents of the Cat class. . .
  • --------------------------------------------------
    ------------------------
  • public class CatDriver
  • Cat fluffy new Cat( 3 )
  • ObjectOutputStream outStream new
    ObjectOutputStream
  • ( new
    FileOutputStream( acatfile.txt ) )
  • outStream.writeObject(fluffy)

16
Assignment
  • Make all of your Shape classes Serializable
  • Write a Driver that Instantiates a Line, a
    Rectangle and a Circle.
  • Serialize them to a file ( use a path that would
    work on any operating system)
  • Read the objects back in and then display their
    points on the screen.

17
Lab
  • Streams I/O lab
Write a Comment
User Comments (0)
About PowerShow.com