Streams
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
Streams are used to transfer the data between program and
source/destination. They transfer the data in unique way
irrespective of source/destination. Streams are defined in
java.io package in java.
Depending up on the direction of the transfer the streams are
classified in to two categories.
Input Stream:
Output Stream
Program Source 
read()
Program Destination 
write ()
SSBN Degree College, ATP M Vishnuvardhan
Introduction
Depending up on how the streams carry the data, they classified
in to two
Byte Streams
These streams carry the data in the form of bytes. They use 8
bit (1 byte) of storage to read the data
Character Streams
These streams carry the data in the form of characters. They
use 2 bytes storage
SSBN Degree College, ATP M Vishnuvardhan
InputStream methods
Method name Description
int read():
Reads next byte from the stream as
integer and returns -1 if no data is
available in the stream
int read(byte b[])
Reads an array full of bytes from the
stream and returns actual number of
bytes read.
int read(byte b[], int start, int end)
Reads bytes in to array from the specified
start and end position form the stream.
long available()
Returns how many number of bytes yet to
be read in the stream.
SSBN Degree College, ATP M Vishnuvardhan
InputStream methods
Method name Description
long skip(long n)
Skips specified number of bytes in the
input stream and returns actual number of
bytes skipped
void mark(int readLimit)
Marks the current position and it is valid
till specified read limit.
void reset()
Moves to the recent marked position or
beginning of the stream
void close() Closes the stream
SSBN Degree College, ATP M Vishnuvardhan
Byte Input Streams
FileInputStream
PipedInputStream
ByteArrayInputStream StringBufferInputStream
ObjectInputStream
SequenceInputStream
InputStream
FilterInputStream
BufferedInputStream PushBackInputStream
DataInputStream
SSBN Degree College, ATP M Vishnuvardhan
Various Byte Input Streams
Stream Class Name Use
FileInputStream used to read from files
PipedInputStream used to read from pipes
ByteArrayInputStream used to read from a byte array
StringBufferInputStream used to read from a String buffer object
ObjectInputStream used to read objects from an input stream
SequenceInputStream used to combine two or more input streams
BufferedInputStream provides buffer facility to the input stream
DataInputStream used to read primitive data from the input
stream
PushBackInputStream provides un reading facility to the input
stream
SSBN Degree College, ATP M Vishnuvardhan
Byte Output Streams
FileOutputStream
PipedOutputStream ByteArrayOutputStream
ObjectOutputStream
OutputStream
FilterOutputStream
BufferedOutputStream
DataOutputStream
PrintStream
SSBN Degree College, ATP M Vishnuvardhan
OutputStream methods
Method name Description
void write(int b) Writes one byte to output stream
void write(byte b[])
Writes an array full of bytes to output
stream
void write(byte b[], int start, int end)
Writes bytes from array to output
stream from the specified start and end
position
void flush()
Flushes the output stream i.e.,
immediately releases the pending data
from stream
void close()
Closes the output stream
SSBN Degree College, ATP M Vishnuvardhan
Byte Output Streams
Stream Class Name Use
FileOutputStream used to write data into a file
PipedOutputStream used to write data to a pipe
ByteArrayOutputStream used to write data to a byte array
ObjectOutputStream used to write objects to a output stream
BufferedOutputStream provides buffer facility to the output stream
DataOutputStream used to write primitive data to an input
stream
PrintStream Used to print any data on output stream
SSBN Degree College, ATP M Vishnuvardhan
Character Input Streams
FileReader
PipedReader
CharArrayReader
InputStreamReader
StringReader
Reader
FilterReader
BufferedReader
PushBackReader
SSBN Degree College, ATP M Vishnuvardhan
Reader methods
Method name Description
int read():
Reads next character from the stream as
integer and returns -1 if no data is
available in the stream.
int read(char c[])
Reads an array full of characters from the
stream and returns actual number of
characters read
int read(char c[], int start, int end)
Reads characters in to array from the
specified start and end position form the
stream
long available()
Returns how many number of bytes yet to
be read in the stream.
SSBN Degree College, ATP M Vishnuvardhan
Reader methods
Method name Description
long skip(long n)
Skips specified number of bytes in the
input stream and returns actual number of
bytes skipped
void mark(int readLimit)
Marks the current position and it is valid
till specified read limit.
void reset()
Moves to the recent marked position or
beginning of the stream
void close() Closes the stream
SSBN Degree College, ATP M Vishnuvardhan
Character Input Streams
Stream Class Name Use
FileReader used to read from files
PipedReader used to read from pipes
CharArrayReader used to read from a char array
StringReader used to read from a String
InputStreamReader used to convert byte stream to character
stream
BufferedReader provides buffer facility to the Reader
PushBackReader provides un reading facility to the Reader
SSBN Degree College, ATP M Vishnuvardhan
Character Output Streams
FileWriter
PipedWriter
CharArrayWriter
OutputStreamWriter
StringWriter
Writer
FilterWriter
BufferedWriter
PrintWriter
SSBN Degree College, ATP M Vishnuvardhan
Writer methods
Method name Description
void write(int c) Writes one char to output stream
void write(char c[])
Writes an array full of chars to output
stream
void write(char c[], int start, int end)
Writes chars from array to output stream
from the specified start and end position
void flush()
Flushes the output stream i.e.,
immediately releases the pending data
from stream
void close()
Closes the output stream
SSBN Degree College, ATP M Vishnuvardhan
Character Output Streams
Stream Class Name Use
FileWriter used to write data into a file
PipedWriter used to write data to a pipe
CharArrayWriter used to write data to a byte array
StringWriter used to write string to a Writer
PrintWriter used to print any data on Writer
BufferedWriter provides buffer facility to the Writer
OutputStreamWriter used to convert character stream to byte
stream
SSBN Degree College, ATP M Vishnuvardhan
Exceptions
 FileNotFoundException
Raises when an attempt is made to open a file which doesnot
exist physically on the disk
 IOException
 Raises when
SSBN Degree College, ATP M Vishnuvardhan
File class
File is a not a stream class but it is part of java.io package which is
used to provide support for files and directories.
Constructors:
File(String fileName):
Constructs a file object with full path of the file
Eg: File f1=new File (“D:ProgramsJavaFileDemo.java”);
File(String parent, String fileName)
Constructs a file object for the file at specified path
Eg: File f2=new File (“D:ProgramJava”,”FileDemo.java”);
SSBN Degree College, ATP M Vishnuvardhan
File Methods:
 String getName(): Returns the name of the file
 String getPath(): Returns path of the file
 boolean isFile(): Returns true if the file object is
a file otherwise false is returned
 boolean isDirectory(): Returns true if the file
object is a directory
 long length(): Returns the size of the file in bytes
 String list[]: Returns an array of strings
representing the files present in the
directory
SSBN Degree College, ATP M Vishnuvardhan
Reading & writing files
 Reading / Writing Bytes
 FileInputStream
 FileOutputStream
 Reading / Writing Characters
 FileReader
 FileWriter
 Reading / Writing Primitive data types
 DataInputStream
 DataOutputStream
SSBN Degree College, ATP M Vishnuvardhan
Reading & writing files
Using DataInputStream and DataOutputStream
FileInputStream fis=new FileInputStream(“Student.txt”);
DataInputStream dis=new DataInputStream(fis);
FileOutputStream fos=new FileOutputStream(“Student.txt”);
DataOutputStream dos=new DataOutputStream(fos);
FileInputStream
File Program
DataInputStream
FileOutputStream
File Program
DataOutputStream
SSBN Degree College, ATP M Vishnuvardhan
Questions

Java Streams

  • 1.
    Streams In JAVA -M Vishnuvardhan, Dept.of Computer Science, SSBN Degree College, ATP
  • 2.
    SSBN Degree College,ATP M Vishnuvardhan Introduction Streams are used to transfer the data between program and source/destination. They transfer the data in unique way irrespective of source/destination. Streams are defined in java.io package in java. Depending up on the direction of the transfer the streams are classified in to two categories. Input Stream: Output Stream Program Source  read() Program Destination  write ()
  • 3.
    SSBN Degree College,ATP M Vishnuvardhan Introduction Depending up on how the streams carry the data, they classified in to two Byte Streams These streams carry the data in the form of bytes. They use 8 bit (1 byte) of storage to read the data Character Streams These streams carry the data in the form of characters. They use 2 bytes storage
  • 4.
    SSBN Degree College,ATP M Vishnuvardhan InputStream methods Method name Description int read(): Reads next byte from the stream as integer and returns -1 if no data is available in the stream int read(byte b[]) Reads an array full of bytes from the stream and returns actual number of bytes read. int read(byte b[], int start, int end) Reads bytes in to array from the specified start and end position form the stream. long available() Returns how many number of bytes yet to be read in the stream.
  • 5.
    SSBN Degree College,ATP M Vishnuvardhan InputStream methods Method name Description long skip(long n) Skips specified number of bytes in the input stream and returns actual number of bytes skipped void mark(int readLimit) Marks the current position and it is valid till specified read limit. void reset() Moves to the recent marked position or beginning of the stream void close() Closes the stream
  • 6.
    SSBN Degree College,ATP M Vishnuvardhan Byte Input Streams FileInputStream PipedInputStream ByteArrayInputStream StringBufferInputStream ObjectInputStream SequenceInputStream InputStream FilterInputStream BufferedInputStream PushBackInputStream DataInputStream
  • 7.
    SSBN Degree College,ATP M Vishnuvardhan Various Byte Input Streams Stream Class Name Use FileInputStream used to read from files PipedInputStream used to read from pipes ByteArrayInputStream used to read from a byte array StringBufferInputStream used to read from a String buffer object ObjectInputStream used to read objects from an input stream SequenceInputStream used to combine two or more input streams BufferedInputStream provides buffer facility to the input stream DataInputStream used to read primitive data from the input stream PushBackInputStream provides un reading facility to the input stream
  • 8.
    SSBN Degree College,ATP M Vishnuvardhan Byte Output Streams FileOutputStream PipedOutputStream ByteArrayOutputStream ObjectOutputStream OutputStream FilterOutputStream BufferedOutputStream DataOutputStream PrintStream
  • 9.
    SSBN Degree College,ATP M Vishnuvardhan OutputStream methods Method name Description void write(int b) Writes one byte to output stream void write(byte b[]) Writes an array full of bytes to output stream void write(byte b[], int start, int end) Writes bytes from array to output stream from the specified start and end position void flush() Flushes the output stream i.e., immediately releases the pending data from stream void close() Closes the output stream
  • 10.
    SSBN Degree College,ATP M Vishnuvardhan Byte Output Streams Stream Class Name Use FileOutputStream used to write data into a file PipedOutputStream used to write data to a pipe ByteArrayOutputStream used to write data to a byte array ObjectOutputStream used to write objects to a output stream BufferedOutputStream provides buffer facility to the output stream DataOutputStream used to write primitive data to an input stream PrintStream Used to print any data on output stream
  • 11.
    SSBN Degree College,ATP M Vishnuvardhan Character Input Streams FileReader PipedReader CharArrayReader InputStreamReader StringReader Reader FilterReader BufferedReader PushBackReader
  • 12.
    SSBN Degree College,ATP M Vishnuvardhan Reader methods Method name Description int read(): Reads next character from the stream as integer and returns -1 if no data is available in the stream. int read(char c[]) Reads an array full of characters from the stream and returns actual number of characters read int read(char c[], int start, int end) Reads characters in to array from the specified start and end position form the stream long available() Returns how many number of bytes yet to be read in the stream.
  • 13.
    SSBN Degree College,ATP M Vishnuvardhan Reader methods Method name Description long skip(long n) Skips specified number of bytes in the input stream and returns actual number of bytes skipped void mark(int readLimit) Marks the current position and it is valid till specified read limit. void reset() Moves to the recent marked position or beginning of the stream void close() Closes the stream
  • 14.
    SSBN Degree College,ATP M Vishnuvardhan Character Input Streams Stream Class Name Use FileReader used to read from files PipedReader used to read from pipes CharArrayReader used to read from a char array StringReader used to read from a String InputStreamReader used to convert byte stream to character stream BufferedReader provides buffer facility to the Reader PushBackReader provides un reading facility to the Reader
  • 15.
    SSBN Degree College,ATP M Vishnuvardhan Character Output Streams FileWriter PipedWriter CharArrayWriter OutputStreamWriter StringWriter Writer FilterWriter BufferedWriter PrintWriter
  • 16.
    SSBN Degree College,ATP M Vishnuvardhan Writer methods Method name Description void write(int c) Writes one char to output stream void write(char c[]) Writes an array full of chars to output stream void write(char c[], int start, int end) Writes chars from array to output stream from the specified start and end position void flush() Flushes the output stream i.e., immediately releases the pending data from stream void close() Closes the output stream
  • 17.
    SSBN Degree College,ATP M Vishnuvardhan Character Output Streams Stream Class Name Use FileWriter used to write data into a file PipedWriter used to write data to a pipe CharArrayWriter used to write data to a byte array StringWriter used to write string to a Writer PrintWriter used to print any data on Writer BufferedWriter provides buffer facility to the Writer OutputStreamWriter used to convert character stream to byte stream
  • 18.
    SSBN Degree College,ATP M Vishnuvardhan Exceptions  FileNotFoundException Raises when an attempt is made to open a file which doesnot exist physically on the disk  IOException  Raises when
  • 19.
    SSBN Degree College,ATP M Vishnuvardhan File class File is a not a stream class but it is part of java.io package which is used to provide support for files and directories. Constructors: File(String fileName): Constructs a file object with full path of the file Eg: File f1=new File (“D:ProgramsJavaFileDemo.java”); File(String parent, String fileName) Constructs a file object for the file at specified path Eg: File f2=new File (“D:ProgramJava”,”FileDemo.java”);
  • 20.
    SSBN Degree College,ATP M Vishnuvardhan File Methods:  String getName(): Returns the name of the file  String getPath(): Returns path of the file  boolean isFile(): Returns true if the file object is a file otherwise false is returned  boolean isDirectory(): Returns true if the file object is a directory  long length(): Returns the size of the file in bytes  String list[]: Returns an array of strings representing the files present in the directory
  • 21.
    SSBN Degree College,ATP M Vishnuvardhan Reading & writing files  Reading / Writing Bytes  FileInputStream  FileOutputStream  Reading / Writing Characters  FileReader  FileWriter  Reading / Writing Primitive data types  DataInputStream  DataOutputStream
  • 22.
    SSBN Degree College,ATP M Vishnuvardhan Reading & writing files Using DataInputStream and DataOutputStream FileInputStream fis=new FileInputStream(“Student.txt”); DataInputStream dis=new DataInputStream(fis); FileOutputStream fos=new FileOutputStream(“Student.txt”); DataOutputStream dos=new DataOutputStream(fos); FileInputStream File Program DataInputStream FileOutputStream File Program DataOutputStream
  • 23.
    SSBN Degree College,ATP M Vishnuvardhan Questions