IOStreams by Kamalakar Dandu

Embed Size (px)

Citation preview

  • 8/2/2019 IOStreams by Kamalakar Dandu

    1/58

  • 8/2/2019 IOStreams by Kamalakar Dandu

    2/58

    Streams: An Intro

    Programs need to send/receiveinformation from a variety of sources file on a disk,

    data on a network,

    data in memory,

    data in another program.

    Types of data: values, characters, image files

    sound files

    If the application wants to receive/senddata, it creates a stream, and reads/write to

    the stream

  • 8/2/2019 IOStreams by Kamalakar Dandu

    3/58

    InputStreams Hierarchy

    InputStream

    FileInputStream

    ByteArrayInputStream

    FilterInputStream

    SequenceInputStream PipedInputStream

    AbstractClass extendsPublic class

    ObjectInputStream

    All these streams handle bytes

  • 8/2/2019 IOStreams by Kamalakar Dandu

    4/58

    Streams: An Intro...

    Create Streams Add data java streams to stream

    read char(byte) from stream

    s

    read char(byte) from stream

    InputStreams are those streams that have a readingend

    and the writing endis connected to a source which

    supplies data (e.g. a HARD DISK).

    Writing endreading end

    j v a s t rj e a m

    a v a s t r e a m sa

    s t r e a m sa sourceva

  • 8/2/2019 IOStreams by Kamalakar Dandu

    5/58

    InputStream Class

    InputStream

    abstract int read()

    int read(byte b[])

    int read(byte b[], int off, int len)long skip(long n)

    int available()

    void close()

    void mark(int readlimit)void reset()

    boolean markSupported()

    class Name

    public

    methods

  • 8/2/2019 IOStreams by Kamalakar Dandu

    6/58

    FileInputStream Class

    FileInputStream extends InputStream

    FileInputStream(File)

    FileInputStream(FileDescriptor)

    FileInputStream(String)native int read()

    native long skip(long n)

    native int available()

    native void close()int read(byte b[])

    int read(byte b[], int off, int len)

    class Name

    public

    methods

  • 8/2/2019 IOStreams by Kamalakar Dandu

    7/58

    InputStream

    int read()

    int read(byte b[], int off, int len )

    void mark(int)

    long skip(long n)

    int available()

    void close()

    int read(byte b[])

    void reset()

    FileInputStream

    int read()int read(byte b[], int off, int len )

    long skip(long n)

    int available() void close()

    int read(byte b[])

    ex

    tends

    abstract methods Native Methods

  • 8/2/2019 IOStreams by Kamalakar Dandu

    8/58

    fis = new FileInputStream();

    Using FileInputStream...

    HD

    system dependent

    resources

    Writing end reading end

    fisis the handle to the stream. Onecan only read from this streamfis.read()will only read fromthe file in the hard disk(HD)

  • 8/2/2019 IOStreams by Kamalakar Dandu

    9/58

    Reading through

    Streams Reading

    open a stream (FileStream)

    while more information { readinformation }

    close the stream

  • 8/2/2019 IOStreams by Kamalakar Dandu

    10/58

    Example

    Read from file ( given as string inthe command line )

    then display contents on theconsole

    Eg.FileInputDemo.java

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    11/58

    OutputStreams Hierarchy

    OutputStream

    FileOutputStream

    ByteArrayOutputStream

    FilterOutPutStream

    PipedOutputStream

    AbstractClass extendsPublic class

    ObjectOutputStream

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    12/58

    OutputStream Class

    OutputStream

    abstract void write(int);

    void write(byte[]);

    void write(byte[], int, int);void flush();

    void close();

    class Name

    public

    methods

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    13/58

    FileOutputStream Class

    FileOutputStream extends OutputStream

    FileOutputStream(String);

    FileOutputStream(String,boolean);

    FileOutputStream(File);FileOutputStream(FileDescriptor);

    native void write(int);

    native void close();

    void write(byte[]);void write(byte[], int, int);

    FileDescriptor getFD();

    class Name

    public

    methods

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    14/58

    fos = new FileOutputStream();

    FileOutputStream...

    HD

    system dependent

    resources

    reading endwriting end

    fis.write()will only write to thefile in the hard disk(HD)

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    15/58

    WritingthroughStreams

    Writing

    Open a stream (FileStream)

    while more information { write information }Close the stream

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    16/58

    Example

    Read form the console and

    Write it to a file (given as string in the command line).

    Eg: FileOutputDemo.java

    http://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileInputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    17/58

    Class File

    Uses of File class :

    To extract information about filesand directories of the local filesystem

    construct an object and use its

    methods to obtain informationabout the files/dirs on the local filesystem

    http://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    18/58

    Classjava.io.File

    File

    File (String thePath)

    File (String , String fname)

    File (File dir, String fname)

    String getAbsolutePath()

    String getCanonicalPath()

    boolean exists()/ canRead()/canWrite()

    long length()/lastModified();boolean isFile()/isDirectory()

    boolean delete()/ renameTo(File)

    class Name

    public

    methods

    http://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    19/58

    Exercise

    Write an application in java to displaylist of the directories or file contents of

    the specified path Textfield should be present at the top, in

    which we can

    specify the file path. Once the file/dir in the textfield is

    specified. It should display info about

    the same in a textArea which is present

    Eg FileTest.java

    http://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileOutputDemo.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    20/58

    ByteArrayOutputStreams

    application

    Internal storage

    ByteArrayOutputStream

    baos.write(int)boas

    ByteArrayOutputStream baos=new

    ByteArrayOutputStream();

    // retrieve data from stream

    byte b[] = baos.toByteArray();

    b

    23 93143 54 155 length=5

    http://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    21/58

    ByteArrayInputStream

    application

    Internal storage

    ByteArrayInputStream

    S.o.p(baos.read())

    bais

    ByteArrayInputStream bais=new

    ByteArrayInputStream(byte[]);

    23 93143 54 155

    14354

    S.o.p(baos.read())

    http://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    22/58

    protected

    InputStream in public int read()

    throws IOException {

    return in.read();}Implements all methods

    by calling the methods

    of the stream using

    handle in

    InputStream

    FilterInputStream

    BufferedInputStreamDataInputStream

    LineNumberInputStreamPushbackInputStream

    buffers input

    give line nums for

    chars:deprecated

    allow bytes to be

    sent back to

    interface

    DataInput

    write primitivejava data types

    http://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    23/58

    InputStreams

    Streams available and their

    functionalityapplication application

    Hard Disk Array of bytes

    ByteArrayInputStreamFileInputStream

    http://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    24/58

    fis

    application

    Hard Disk

    FileInputStream

    BufferedInputStream

    FileInputStream fis=new FileInputStream

    (test.dat);

    BufferedInputStream bis=new

    BufferedInputStream (fis);

    bis.read()

    bisRequire another input stream asa parameter

    BufferedInputStream

    E.gBufferedTextInputTest.java

    http://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/FileTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    25/58

    BufferedInputStreamClass

    BufferedInputStream extends FilterStreamBufferedInputStream(InputStream)

    BufferedInputStream(InputStream,int)

    int available()

    long skip(long n)

    void close()

    void mark(int)

    void reset()int read(byte b[])

    int read(byte b[], int off, int len)

    class Namepublic

    methods

    Allows the use ofmark, reset and

    other methods onthe inputstream

    http://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    26/58

    Example

    Read one fourth from file ( givenas string in the command line ),and then display contents on the

    console. Reset it and thendisplay the first one fourth of thecontents again on the console.Eg: TestBufferedStream.java

    http://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/BufferedTextInputTest.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    27/58

    DataInputStream

    int readInt()

    int read(byte b[], int

    off, int len )

    boolean readBoolean()

    float readFloat()

    long readLong()

    int read(byte b[])

    char readChar()

    byte readByte()

    String readLine()

    short readShort()

    int readUnsignedByte() int readUnsignedShort()

    DataInput

    String readUTF()

    double readDouble()

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    28/58

    DataInput/OutputStreams

    Read and write primitive datatypes in bit streams (actually

    stored as bytes)

    read and write in a machine

    independent way Data must be written and read in

    the same order

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    29/58

    DataOutputStream dos=new DataOutputStream (fos);

    HD

    FileOutputStream fos=new FileOutputStream (testdata.dat);

    dos.writeboolean(true); dos.close(); fos.close();

    DataInputStream dis=new DataInputStream (fis);FileInputStream fis=new FileInputStream (testdata.dat);

    System.out.println(dis.readBoolean());

    Information stored internally as bit/byteInternal information not available to

    programmer

    DataInput/OutputStreams

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    30/58

    Example

    Write a program to write the following information to a file

    named testItem.dat (items separated by tabs)

    25.99 12 Tennis racket

    345.32 6 T-shirtsWrite a second program to read and display the above

    information from the file testItem.dat

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    31/58

    PrintStream

    Deprecated

    use PrintWriter instead

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    32/58

    Class System

    An uninstantiable System class thatprovides a lot of important methods(all static) and references for otherobjects InputStream in

    OutputStream out

    OutputStream err

    exit() gc()

    arrayCopy

    getProperties

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    33/58

    Reading from Keyboard

    A handle to the Keyboard data isgiven to the user as a InputStream(System.in)

    Programmer needs to encapsulate itwith higher level streams to obtainand process inputs

    Use reader to trap characters, andthen process them depending as towhat the application requires

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    34/58

    application

    String str = br.readLine()

    br BufferedReader br=new BufferReader(new InputStreamReader(System.in) );

    //process to obtain an integer

    int i = Integer.parseInt(str);

    Operating System Control returns after user

    presses enter keyEg: KeyInput.java

    http://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/TestBufferedStream.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    35/58

    Redirecting Output to File

    Use setOut(OutputStream ) ofclass Systemto set the output

    stream of the app

    consequent commands to print()and println() are set to thatstream that was passed tosetOut

    http://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    36/58

    applicationout

    //write to System.out

    System.out.println(java class);

    Operating System

    Normal Application

    http://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    37/58

  • 8/2/2019 IOStreams by Kamalakar Dandu

    38/58

    Example

    If the user gives a filename at thecommand prompt, then the

    application will redirect output tothat file, and also to the console

    if the user does not give a

    filename, the application willwrite only to the console insteadof directing data to the file

    Eg: RedirectOutput.java

    http://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/KeyInput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    39/58

    Streams Heirarchy

    Reader

    BufferedReader

    InputStreamReader

    StringReader

    CharArrayReaderPipedReader

    AbstractClass extendsPublic class

    FilterReader

    All these streams handle chars

    FileReader PushbackReader

    http://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    40/58

    Streams Heirarchy

    Writer

    BufferedWriter

    OuputStreamWriter

    StringWriter

    CharArrayWriterPipedWriter

    AbstractClass extendsPublic class

    FilterWriter

    FileReader PrintWriter

    http://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    41/58

    RandomAccessFile

    Implements DataOut and DataInputIt Supports both Reading and writing to a file

    Constructors:

    RandomAccessFile(File file, String mode)

    RandomAccessFile(String name, String mode)

    The mode argument must either be equal to "r"

    or "rw", indicating that the file is to be openedfor input only or for both input and output,

    respectively.

    http://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://iostreams/RedirectOutput.javahttp://../java/io/RandomAccessFile.htmlhttp://../java/io/File.htmlhttp://../java/lang/String.htmlhttp://../java/io/RandomAccessFile.htmlhttp://../java/lang/String.htmlhttp://../java/lang/String.htmlhttp://../java/lang/String.htmlhttp://../java/lang/String.htmlhttp://../java/io/RandomAccessFile.htmlhttp://../java/lang/String.htmlhttp://../java/io/File.htmlhttp://../java/io/RandomAccessFile.html
  • 8/2/2019 IOStreams by Kamalakar Dandu

    42/58

  • 8/2/2019 IOStreams by Kamalakar Dandu

    43/58

    Example

    E.g TextIOTest.java ( Writer andRandomAccessFile )

    E.g ReadStream.java ( Readers & Writersusing Applets )

    Eg ReadStream.javaEg:TestIOTest.java

    http://iostreams/ReadStream.javahttp://iostreams/ReadStream.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/ReadStream.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    44/58

    Object Serialization Converting the state of an object

    into a serial information (binary /bytes).

    Uses

    Information can be sent over thenetwork to be reconstructed at theother end (rmi)

    stored in a hard disk as a file, andthe object state can bereconstructed much after theobject has been removed from

    memory (i.e persistence)

    http://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    45/58

    Serializing Java

    Objects Object serialization is achieved

    through ObjectInputStream and

    ObjectOutputStream

    ObjectOutputStream is used to

    write to a stream

    ObjectInputStream is used to readfrom stream

    class Name

    http://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    46/58

    class Name

    public

    methods

    ObjectOutputStreamObjectOutputStream(OutputStream)

    final void writeObject(Object)

    write(byte[]); write(byte[], int, int)

    writeBoolean(boolean)writeByte(int); writeBytes(String)

    writeChar(int); writeChars(String)

    writeFloat(float); writeDouble(double)

    writeFields()

    write(int); writeInt(int)

    writeShort(int); writeLong(long)

    writeUTF(String);

    defaultWriteObject();

    flush(); close()

    If you want to store theobject stateto a file, pass aFileInputStream handlehere

    class ObjectOutputStream

    http://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    47/58

    Serializing Java Objects

    Steps to serialize object data to a stream

    create an ObjectOutputStream

    create object to be serialized (e.g.. Stringobject)

    use the writeObject method with the objectreference as a parameter

    ObjectOutputStream oos = new OOS();

    String st = new String(java serialization);

    oos.writeObject(st);

    If you want to store theobject stateto a file, pass a

    FileInputStream handlehere

    http://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    48/58

    Steps to deserialize object state fromstream (reading from the stream)

    create an ObjectInputStream

    define type of handle for object

    use the readObject method which returns thethe object reference to the serialized object as

    an java Object reference typecast it back to the correct object type

    sequence of reading is same as that of writing

    Serializing Java Objects

    Methods for input stream matchmethods in outputstream

    OutputStream InputStreamwriteChar(char) char readChar()

    ObjectInputStream ois = new OIS();String st;st = (String)ois.readObject();

    http://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    49/58

    Example

    Write a program to store a string followed by a

    vector that stores james bond, 007, last line

    into a file objdata.txt (this example shows

    serializing the String object and the Vector object)Write a second program to read the file objdata.txt,

    and display the contents on the console

    Eg: OOSPoint. java

    http://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/TextIOTest.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    50/58

    Serializing Custom Classes

    An object (written by the programmer)becomes serializable by implementingthe Serializableinterface (java.io

    package, and it contains no methods) Serialization is then taken care of by the

    defaultWriteObject(...) method of the

    ObjectOutputStream class Deserialization is similarly taken care of

    by the defaultReadObject(...) of the

    ObjectInputStream class

    http://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.javahttp://iostreams/oosPoint.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    51/58

    S i li i C Cl

    http://iostreams/SerialPoint.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    52/58

    Serializing Custom Classes

    If you want to add additional informationto the stream (to stream / file), then onehas to override the writeObject() method

    in their class with signature private void

    writeObject(ObjectOutputStream) throws

    IOException; you will need to call the defaultWriteObject

    to serialize the data of your class

    after that you can add whatever data you

    want to be serialized alongwith the class

    http://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    53/58

    Serializing Custom Classes

    In such cases they will have toread data in the same reverse

    order and override thereadObject() method in theirclass

    private voidreadObject(ObjectInputStream

    stream) throws IOException;

    http://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    54/58

    Example

    Write some String afterserializing the object , read theinfo stored after de-serializing ina separate variable (initially setto null)

    Eg: NewPoint.java

    Eg: SerialNewPoint.java

    File Channel

    http://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/SerialPoint.javahttp://iostreams/newPoint.javahttp://iostreams/SerialNewPoint.javahttp://iostreams/SerialNewPoint.javahttp://iostreams/newPoint.javahttp://iostreams/SerialNewPoint.java
  • 8/2/2019 IOStreams by Kamalakar Dandu

    55/58

    File ChannelA channel for reading, writing, mapping, andmanipulating a file.

    This Slid concentrates on file locking mechanisms.

    FileChannel.lock()Acquires an exclusive lock on this

    channel's file.

    FileChannel.lock(long position, long size,boolean shared)

    Acquires a lock on the given region of this

    channel's file.

    The above methods return a FileLock Object.

    Method : FileLock.release()

    FileLockDemo.java

    http://iostreams/SerialNewPoint.javahttp://iostreams/SerialNewPoint.javahttp://iostreams/SerialNewPoint.javahttp://iostreams/SerialNewPoint.javahttp://iostreams/SerialNewPoint.javahttp://d/j2sdk-1_4_1-doc/docs/api/java/nio/channels/FileChannel.htmlhttp://d/j2sdk-1_4_1-doc/docs/api/java/nio/channels/FileChannel.htmlhttp://d/j2sdk-1_4_1-doc/docs/api/java/nio/channels/FileChannel.htmlhttp://d/j2sdk-1_4_1-doc/docs/api/java/nio/channels/FileChannel.html
  • 8/2/2019 IOStreams by Kamalakar Dandu

    56/58

    E i ( d )

  • 8/2/2019 IOStreams by Kamalakar Dandu

    57/58

    Exercise (contd)

    16. A need to persist the data has now arisen asthe client observes that the development team iseither getting the data from the console or is

    having it stored as string attributes in theprograms. The client wishes that the data shouldbe persisted and reused by any other programs,as well. Hence, store the data in the Collection

    into a file. Implement methods to read/write thedata to/from the given file. The access methodsshould be synchronized

  • 8/2/2019 IOStreams by Kamalakar Dandu

    58/58