KEMBAR78
Byte stream classes.49
ByteStream classes




    http://improvejava.blogspot.in/   1
Objective

On completion of this period, you would be able to
 learn
   • ByteStream classes




                    http://improvejava.blogspot.in/   2
Recap

In 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
Types of Stream Class

Java’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
Byte Streams

Byte Streams are two types

   1.   InputStream class
   2.   OutputStream class




                     http://improvejava.blogspot.in/   5
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
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
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
// Program for FileInputStream
import 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
// Program for FileInputStream
byte b[] = new byte[n];
if (f.read(b) != n) {
System.err.println("couldn't 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("couldn't 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
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
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
Quiz

1. All methods in InputStream class will throw an
   IOException

   A. True
   B. False




                   http://improvejava.blogspot.in/   13
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
Frequently Asked Questions

1. Write about Byte Stream classes
2. Write a java program which illustrates the use of
   FileInputStream




                   http://improvejava.blogspot.in/     15

Byte stream classes.49

  • 1.
    ByteStream classes http://improvejava.blogspot.in/ 1
  • 2.
    Objective On completion ofthis period, you would be able to learn • ByteStream classes http://improvejava.blogspot.in/ 2
  • 3.
    Recap In the lastclass, 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.
    Types of StreamClass Java’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.
    Byte Streams Byte Streamsare two types 1. InputStream class 2. OutputStream class http://improvejava.blogspot.in/ 5
  • 6.
    InputStream class • Datais 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.
    OutputStream class • Isan 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.
    FileInputStream class • Isa 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.
    // Program forFileInputStream import 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.
    // Program forFileInputStream byte b[] = new byte[n]; if (f.read(b) != n) { System.err.println("couldn't 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("couldn't 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.
    FileOutputStream class • Isa 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.
    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.
    Quiz 1. All methodsin InputStream class will throw an IOException A. True B. False http://improvejava.blogspot.in/ 13
  • 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.
    Frequently Asked Questions 1.Write about Byte Stream classes 2. Write a java program which illustrates the use of FileInputStream http://improvejava.blogspot.in/ 15