Java I/O 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

File Handling in java.

Transcript of Java I/O Part 2

Page 1: Java I/O 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: Java I/O 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: Java I/O Part 2

Overview of IO Classes

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

Page 4: Java I/O Part 2

OutputStream of IO

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

Page 5: Java I/O Part 2

InputStream of IO

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

Page 6: Java I/O Part 2

Writer of IO

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

Page 7: Java I/O Part 2

Reader of IO

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

Page 8: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O Part 2

InputStream Usage

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

Page 42: Java I/O Part 2

OutputStream Usage

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

Page 43: Java I/O 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: Java I/O Part 2

OutputStreamWriter

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

Page 45: Java I/O Part 2

InputStream Reader

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

Page 46: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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: Java I/O 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