The document discusses byte stream classes in Java. There are two types of byte streams: InputStream and OutputStream. InputStream provides methods for reading bytes of data sequentially. FileInputStream and FileOutputStream are subclasses that allow reading and writing bytes from/to files. FileInputStream can be constructed using a file path or File object and overrides InputStream methods like read() to access file bytes.
In this document
Powered by AI
Introduction to ByteStream classes and learning objectives related to them.
Recap on the File class and its properties discussed in the previous session.
Overview of the four abstract classes in Java's stream-based I/O: InputStream, OutputStream, Reader, Writer.
Introduction to Byte Streams, detailing InputStream and OutputStream classes.
Introduction to FileInputStream class and its constructors.
Java program demonstrating the use of FileInputStream to read byte data from files.
Introduction to FileOutputStream class, its constructors, and potential exceptions.
Summary of Byte Streams, focusing on InputStream and OutputStream and their subclasses.
Quiz questions to test knowledge on InputStream class methods and functionality.
Frequently asked questions about Byte Stream classes and examples in Java.
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