Byte stream classes.49

2,433 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,433
On SlideShare
0
From Embeds
0
Number of Embeds
146
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Byte stream classes.49

  1. 1. ByteStream classes http://improvejava.blogspot.in/ 1
  2. 2. ObjectiveOn completion of this period, you would be able to learn • ByteStream classes http://improvejava.blogspot.in/ 2
  3. 3. RecapIn the last class, you have studied about the File class• You have also studied about the various methods, and properties of a File class http://improvejava.blogspot.in/ 3
  4. 4. Types of Stream ClassJava’s stream-based I/O is built upon four abstract classes: 1. Input Stream 2. Output Stream 3. Reader 4. Writer http://improvejava.blogspot.in/ 4
  5. 5. Byte StreamsByte Streams are two types 1. InputStream class 2. OutputStream class http://improvejava.blogspot.in/ 5
  6. 6. InputStream class• Data is accessed as a sequence of bytes• Its an abstract class that defines streaming byte input• The methods in the input stream are used to read bytes from input stream• All methods in this class will throw an I/O Exception http://improvejava.blogspot.in/ 6
  7. 7. OutputStream class• Is an abstract class that defines streaming byte output• The methods of it are used to send bytes to the output stream• All methods in this class return a void value and throw an I/O Exception in the case of errors http://improvejava.blogspot.in/ 7
  8. 8. FileInputStream class• Is a subclass of InputStream that you can use to read bytes from a file• Its has two constructors • FileInputStream(String filePath) • FileInputStream(File fileObj)• When FileInputStream is created, it is also opened for reading• FileInputStream overrides six of the methods in the abstract class InputStream http://improvejava.blogspot.in/ 8
  9. 9. // Program for FileInputStreamimport java.io.*;class FileInputStreamDemo {public static void main(String args[]) throws Exception {int size;InputStream f =new FileInputStream("FileInputStreamDemo.java");System.out.println("Total Available Bytes: " +(size = f.available()));int n = size/40;System.out.println("First " + n +" bytes of the file one read() at a time");for (int i=0; i < n; i++) {System.out.print((char) f.read()); }System.out.println("nStill Available: " + f.available());System.out.println("Reading the next " + n +" with one read(b[])"); http://improvejava.blogspot.in/ 9
  10. 10. // Program for FileInputStreambyte b[] = new byte[n];if (f.read(b) != n) {System.err.println("couldnt read " + n + " bytes."); }System.out.println(new String(b, 0, n));System.out.println("nStill Available: " + (size = f.available()));System.out.println("Skipping half of remaining bytes with skip()");f.skip(size/2);System.out.println("Still Available: " + f.available());System.out.println("Reading " + n/2 + " into the end of array");if (f.read(b, n/2, n/2) != n/2) {System.err.println("couldnt read " + n/2 + " bytes."); }System.out.println(new String(b, 0, b.length));System.out.println("nStill Available: " + f.available());f.close(); } } http://improvejava.blogspot.in/ 10
  11. 11. FileOutputStream class• Is a subclass of OutputStream that you can use to write bytes to a file• Constructors are • FileOutputStream(String filePath) • FileOutputStream(File fileObj) • FileOutputStream(String filePath, boolean append) • FileOutputStream(File fileObj, boolean append)• They can throw a FileNotFoundException or a SecurityException http://improvejava.blogspot.in/ 11
  12. 12. Summary• Byte Streams are two types: InputStream and OutputStream• In InputStream data is accessed as a sequence of bytes• FileInputStream and FilOuputStream are the subclasses of ByteStream http://improvejava.blogspot.in/ 12
  13. 13. Quiz1. All methods in InputStream class will throw an IOException A. True B. False http://improvejava.blogspot.in/ 13
  14. 14. Quiz contd ..2. Which method is used to read bytes from InpuStream A. readLine() B. read() C. write() D. None http://improvejava.blogspot.in/ 14
  15. 15. Frequently Asked Questions1. Write about Byte Stream classes2. Write a java program which illustrates the use of FileInputStream http://improvejava.blogspot.in/ 15

×