I/O In Java Part 2

54
File IO in Java Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 September 21, 2012 Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 1

description

Different streams in java. Different classes and methods in the streams

Transcript of I/O In Java Part 2

Page 1: I/O In Java Part 2

File IO in Java

Prof. AshishSingh Bhatia, [email protected],[email protected], Web: asbspace.in, M:9879009551

September 21, 2012

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 1

Page 2: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 2

Page 3: I/O In Java Part 2

Overview of IO Classes

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 3

Page 4: I/O In Java Part 2

OutputStream of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 4

Page 5: I/O In Java Part 2

InputStream of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 5

Page 6: I/O In Java Part 2

Writer of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 6

Page 7: I/O In Java Part 2

Reader of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 7

Page 8: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 8

Page 9: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 9

Page 10: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 10

Page 11: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 11

Page 12: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 12

Page 13: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 13

Page 14: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 14

Page 15: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 15

Page 16: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 16

Page 17: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 17

Page 18: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 18

Page 19: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 19

Page 20: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 20

Page 21: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 21

Page 22: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 22

Page 23: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 23

Page 24: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 24

Page 25: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 25

Page 26: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 26

Page 27: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 27

Page 28: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 28

Page 29: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 29

Page 30: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 30

Page 31: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 31

Page 32: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 32

Page 33: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 33

Page 34: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 34

Page 35: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 35

Page 36: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 36

Page 37: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 37

Page 38: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 38

Page 39: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 39

Page 40: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 40

Page 41: I/O In Java Part 2

InputStream Usage

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 41

Page 42: I/O In Java Part 2

OutputStream Usage

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 42

Page 43: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 43

Page 44: I/O In Java Part 2

OutputStreamWriter

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 44

Page 45: I/O In Java Part 2

InputStream Reader

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 45

Page 46: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 46

Page 47: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 47

Page 48: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 48

Page 49: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 49

Page 50: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 50

Page 51: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 51

Page 52: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 52

Page 53: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 53

Page 54: I/O In Java Part 2

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, [email protected], [email protected], Web: asbspace.in, M:9879009551 54