Java I/O Part 2

Post on 11-Nov-2014

301 views 3 download

Tags:

description

File Handling in java.

Transcript of Java I/O Part 2

File IO in Java

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com,ashish@asbspace.in, Web: asbspace.in, M:9879009551

September 21, 2012

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 1

AGENDA

Overview of IO Classes

OutputStream of IO

InputStream of IO

Writer of IO

Reader of IO

Methods of OutputStream Class

Methods of Writer Class

Methods of Writer Class Extra

Methods of InputStream Class

Methods of Reader Class

InputStream Usage

OutputStream Usage

Bridge Classes

OutputStreamWriter

InputStream Reader

FileInput and FileOutput StreamProf. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 2

Overview of IO Classes

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 3

OutputStream of IO

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 4

InputStream of IO

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 5

Writer of IO

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 6

Reader of IO

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 7

Methods of OutputStream Class

I public void write(int b)

Writes a single byte to an output stream. Why we need int asargument?

I public void write(byte[] b)

Writes complete byte array in to an outputstream.

I public void write(byte[] b, int offset, int len)

Write a subrange from offset to specified length[number ofbytes].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the outputstream. Further write will generateIOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 8

Methods of OutputStream Class

I public void write(int b)

Writes a single byte to an output stream. Why we need int asargument?

I public void write(byte[] b)

Writes complete byte array in to an outputstream.

I public void write(byte[] b, int offset, int len)

Write a subrange from offset to specified length[number ofbytes].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the outputstream. Further write will generateIOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 9

Methods of OutputStream Class

I public void write(int b)

Writes a single byte to an output stream. Why we need int asargument?

I public void write(byte[] b)

Writes complete byte array in to an outputstream.

I public void write(byte[] b, int offset, int len)

Write a subrange from offset to specified length[number ofbytes].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the outputstream. Further write will generateIOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 10

Methods of OutputStream Class

I public void write(int b)

Writes a single byte to an output stream. Why we need int asargument?

I public void write(byte[] b)

Writes complete byte array in to an outputstream.

I public void write(byte[] b, int offset, int len)

Write a subrange from offset to specified length[number ofbytes].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the outputstream. Further write will generateIOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 11

Methods of OutputStream Class

I public void write(int b)

Writes a single byte to an output stream. Why we need int asargument?

I public void write(byte[] b)

Writes complete byte array in to an outputstream.

I public void write(byte[] b, int offset, int len)

Write a subrange from offset to specified length[number ofbytes].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the outputstream. Further write will generateIOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 12

Methods of Writer Class

I public void write(int ch)

Writes a single character to an output stream. Why we needint as argument?

I public void write(char[] ch)

Writes complete character array in to an outputstream.

I public void write(char[] b, int offset, int len)

Write a subrange from offset to specified length[number ofcharacter].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the writer. Further write will generate IOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 13

Methods of Writer Class

I public void write(int ch)

Writes a single character to an output stream. Why we needint as argument?

I public void write(char[] ch)

Writes complete character array in to an outputstream.

I public void write(char[] b, int offset, int len)

Write a subrange from offset to specified length[number ofcharacter].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the writer. Further write will generate IOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 14

Methods of Writer Class

I public void write(int ch)

Writes a single character to an output stream. Why we needint as argument?

I public void write(char[] ch)

Writes complete character array in to an outputstream.

I public void write(char[] b, int offset, int len)

Write a subrange from offset to specified length[number ofcharacter].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the writer. Further write will generate IOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 15

Methods of Writer Class

I public void write(int ch)

Writes a single character to an output stream. Why we needint as argument?

I public void write(char[] ch)

Writes complete character array in to an outputstream.

I public void write(char[] b, int offset, int len)

Write a subrange from offset to specified length[number ofcharacter].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the writer. Further write will generate IOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 16

Methods of Writer Class

I public void write(int ch)

Writes a single character to an output stream. Why we needint as argument?

I public void write(char[] ch)

Writes complete character array in to an outputstream.

I public void write(char[] b, int offset, int len)

Write a subrange from offset to specified length[number ofcharacter].

I public void flush()

Flushes the output buffer. Ensures the contents are written tothe destination.

I public void close()

Closes the writer. Further write will generate IOException

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 17

Methods of Writer Class Extra

I String is a sequence of Character.

I public void write(String s)

I public void write(String s, int offset, int len)

I public void append(char ch)

I public void append(CharSequence cs)

I public void append(CharSequence cs, int offset,

int len)

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 18

Methods of Writer Class Extra

I String is a sequence of Character.

I public void write(String s)

I public void write(String s, int offset, int len)

I public void append(char ch)

I public void append(CharSequence cs)

I public void append(CharSequence cs, int offset,

int len)

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 19

Methods of Writer Class Extra

I String is a sequence of Character.

I public void write(String s)

I public void write(String s, int offset, int len)

I public void append(char ch)

I public void append(CharSequence cs)

I public void append(CharSequence cs, int offset,

int len)

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 20

Methods of Writer Class Extra

I String is a sequence of Character.

I public void write(String s)

I public void write(String s, int offset, int len)

I public void append(char ch)

I public void append(CharSequence cs)

I public void append(CharSequence cs, int offset,

int len)

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 21

Methods of Writer Class Extra

I String is a sequence of Character.

I public void write(String s)

I public void write(String s, int offset, int len)

I public void append(char ch)

I public void append(CharSequence cs)

I public void append(CharSequence cs, int offset,

int len)

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 22

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 23

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 24

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)

I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 25

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 26

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.

I public void mark(int readlimit)I public void reset()I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 27

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)

I public void reset()I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 28

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()

I public boolean markSupported()I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 29

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 30

Methods of InputStream Class

I public int read()

Read the integer representation of the next available byte ofinput. Returns -1 when the end of file is encountered.

I public int read(byte[] b)

Attempt to read up to b.length bytes in the the b array.Returns the number of actual bytes read. -1 is returned whenthe end of file is encountered.

I public int read(byte[] b, int offset, int len)I public long skip(long count)

count indicates number of bytes to be skipped and return thenumber of byte skipped or -1 in case of end of file.

I public int available()

Number of bytes available for reading.I public void mark(int readlimit)I public void reset()I public boolean markSupported()I public void close()Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 31

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 32

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 33

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 34

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 35

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 36

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 37

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 38

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 39

Methods of Reader Class

I public int read()

I public int read(char[] b)

I public int read(char[] b, int offset, int len)

I public long skip(long count)

I public boolean ready()

I public void mark(int readlimit)

I public void reset()

I public boolean markSupported()

I public void close()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 40

InputStream Usage

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 41

OutputStream Usage

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 42

Bridge Classes

I OutputStreamWriter and InputStreamReader

I Character Base Stream Classes which inturns work on bytes.

I Handles unicode character and bytes both.

I Encoding and Decoding of unicode character from and tovarious character set.

I java.nio.charset package has classes which handleencoding and decoding.

I class Charset represents different character set.

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 43

OutputStreamWriter

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 44

InputStream Reader

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 45

FileInput and FileOutput Stream

I Output and input of binary files.

I FileOutputStream create and write bytes in to physical file.

I If file exists it overwrites it until append flag is used.

I Inherits all the methods from the OutputStream.

I Additional methods arepublic FileDescriptor getFD()

public FileChannel getChannel()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 46

FileInput and FileOutput Stream

I Output and input of binary files.

I FileOutputStream create and write bytes in to physical file.

I If file exists it overwrites it until append flag is used.

I Inherits all the methods from the OutputStream.

I Additional methods arepublic FileDescriptor getFD()

public FileChannel getChannel()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 47

FileInput and FileOutput Stream

I Output and input of binary files.

I FileOutputStream create and write bytes in to physical file.

I If file exists it overwrites it until append flag is used.

I Inherits all the methods from the OutputStream.

I Additional methods arepublic FileDescriptor getFD()

public FileChannel getChannel()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 48

FileInput and FileOutput Stream

I Output and input of binary files.

I FileOutputStream create and write bytes in to physical file.

I If file exists it overwrites it until append flag is used.

I Inherits all the methods from the OutputStream.

I Additional methods arepublic FileDescriptor getFD()

public FileChannel getChannel()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 49

FileInput and FileOutput Stream

I Output and input of binary files.

I FileOutputStream create and write bytes in to physical file.

I If file exists it overwrites it until append flag is used.

I Inherits all the methods from the OutputStream.

I Additional methods arepublic FileDescriptor getFD()

public FileChannel getChannel()

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 50

FileDescriptor

I Whenever any file is opened for input or output, it uses a filedescriptor.

I 0=Standard Input

I 1=Standard Output

I 2=Standard Error

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 51

FileDescriptor

I Whenever any file is opened for input or output, it uses a filedescriptor.

I 0=Standard Input

I 1=Standard Output

I 2=Standard Error

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 52

FileDescriptor

I Whenever any file is opened for input or output, it uses a filedescriptor.

I 0=Standard Input

I 1=Standard Output

I 2=Standard Error

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 53

FileDescriptor

I Whenever any file is opened for input or output, it uses a filedescriptor.

I 0=Standard Input

I 1=Standard Output

I 2=Standard Error

Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 54