I/O In Java Part 2

1,030 views

Published on

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

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,030
On SlideShare
0
From Embeds
0
Number of Embeds
438
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

I/O In Java Part 2

  1. 1. File IO in Java Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 September 21, 2012Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 1
  2. 2. AGENDAOverview of IO ClassesOutputStream of IOInputStream of IOWriter of IOReader of IOMethods of OutputStream ClassMethods of Writer ClassMethods of Writer Class ExtraMethods of InputStream ClassMethods of Reader ClassInputStream UsageOutputStream UsageBridge ClassesOutputStreamWriterInputStream ReaderFileInput and FileOutput Stream Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 2
  3. 3. Overview of IO Classes Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 3
  4. 4. OutputStream of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 4
  5. 5. InputStream of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 5
  6. 6. Writer of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 6
  7. 7. Reader of IO Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 7
  8. 8. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 8
  9. 9. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 9
  10. 10. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. public void write(byte[] b, int offset, int len) Write a subrange from offset to specified length[number of bytes]. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 10
  11. 11. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. public void write(byte[] b, int offset, int len) Write a subrange from offset to specified length[number of bytes]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 11
  12. 12. Methods of OutputStream Class public void write(int b) Writes a single byte to an output stream. Why we need int as argument? public void write(byte[] b) Writes complete byte array in to an outputstream. public void write(byte[] b, int offset, int len) Write a subrange from offset to specified length[number of bytes]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. public void close() Closes the outputstream. Further write will generate IOException Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 12
  13. 13. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 13
  14. 14. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 14
  15. 15. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. public void write(char[] b, int offset, int len) Write a subrange from offset to specified length[number of character]. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 15
  16. 16. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. public void write(char[] b, int offset, int len) Write a subrange from offset to specified length[number of character]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 16
  17. 17. Methods of Writer Class public void write(int ch) Writes a single character to an output stream. Why we need int as argument? public void write(char[] ch) Writes complete character array in to an outputstream. public void write(char[] b, int offset, int len) Write a subrange from offset to specified length[number of character]. public void flush() Flushes the output buffer. Ensures the contents are written to the destination. 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
  18. 18. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 18
  19. 19. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 19
  20. 20. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) public void append(char ch) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 20
  21. 21. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) public void append(char ch) public void append(CharSequence cs) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 21
  22. 22. Methods of Writer Class Extra String is a sequence of Character. public void write(String s) public void write(String s, int offset, int len) public void append(char ch) public void append(CharSequence cs) 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
  23. 23. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 23
  24. 24. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 24
  25. 25. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 25
  26. 26. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 26
  27. 27. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 27
  28. 28. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 28
  29. 29. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) public void reset() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 29
  30. 30. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) public void reset() public boolean markSupported() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 30
  31. 31. Methods of InputStream Class public int read() Read the integer representation of the next available byte of input. Returns -1 when the end of file is encountered. 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 when the end of file is encountered. public int read(byte[] b, int offset, int len) public long skip(long count) count indicates number of bytes to be skipped and return the number of byte skipped or -1 in case of end of file. public int available() Number of bytes available for reading. public void mark(int readlimit) public void reset() public boolean markSupported() public void close() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 31
  32. 32. Methods of Reader Class public int read() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 32
  33. 33. Methods of Reader Class public int read() public int read(char[] b) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 33
  34. 34. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 34
  35. 35. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 35
  36. 36. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 36
  37. 37. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 37
  38. 38. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) public void reset() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 38
  39. 39. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) public void reset() public boolean markSupported() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 39
  40. 40. Methods of Reader Class public int read() public int read(char[] b) public int read(char[] b, int offset, int len) public long skip(long count) public boolean ready() public void mark(int readlimit) public void reset() public boolean markSupported() public void close() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 40
  41. 41. InputStream Usage Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 41
  42. 42. OutputStream Usage Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 42
  43. 43. Bridge Classes OutputStreamWriter and InputStreamReader Character Base Stream Classes which inturns work on bytes. Handles unicode character and bytes both. Encoding and Decoding of unicode character from and to various character set. java.nio.charset package has classes which handle encoding and decoding. class Charset represents different character set. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 43
  44. 44. OutputStreamWriter Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 44
  45. 45. InputStream Reader Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 45
  46. 46. FileInput and FileOutput Stream Output and input of binary files. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 46
  47. 47. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 47
  48. 48. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. If file exists it overwrites it until append flag is used. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 48
  49. 49. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. If file exists it overwrites it until append flag is used. Inherits all the methods from the OutputStream. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 49
  50. 50. FileInput and FileOutput Stream Output and input of binary files. FileOutputStream create and write bytes in to physical file. If file exists it overwrites it until append flag is used. Inherits all the methods from the OutputStream. Additional methods are public FileDescriptor getFD() public FileChannel getChannel() Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 50
  51. 51. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 51
  52. 52. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. 0=Standard Input Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 52
  53. 53. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. 0=Standard Input 1=Standard Output Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 53
  54. 54. FileDescriptor Whenever any file is opened for input or output, it uses a file descriptor. 0=Standard Input 1=Standard Output 2=Standard Error Prof. AshishSingh Bhatia, ast.bhatia@gmail.com, ashish@asbspace.in, Web: asbspace.in, M:9879009551 54

×