http://improvejava.blogspot.in/ 2
Objective
On completion of this period, you would be able to
learn
•ByteStream classes
http://improvejava.blogspot.in/ 3
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/ 4
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/ 5
Byte Streams
Byte Streams are two types
1.InputStream class
2.OutputStream class
http://improvejava.blogspot.in/ 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/ 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/ 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/ 9
// 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/ 10
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(); } }
// Program for FileInputStream
http://improvejava.blogspot.in/ 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/ 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/ 13
Quiz
1.All methods in InputStream class will throw an
IOException
A.True
B.False
2.Which method is used to read bytes from
InpuStream
A.readLine()
B.read()
C.write()
D.None
http://improvejava.blogspot.in/ 14
Quiz contd ..
http://improvejava.blogspot.in/ 15
Frequently Asked Questions
1.Write about Byte Stream classes
2.Write a java program which illustrates the use of
FileInputStream