SlideShare a Scribd company logo
1 of 23
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

More Related Content

What's hot

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

What's hot (20)

String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Java I/O
Java I/OJava I/O
Java I/O
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java threads
Java threadsJava threads
Java threads
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
List in Python
List in PythonList in Python
List in Python
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 

Similar to Java Streams

Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
phanleson
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
SakkaravarthiS1
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
Bharat17485
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
RathanMB
 

Similar to Java Streams (20)

Io Streams
Io StreamsIo Streams
Io Streams
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Java stream
Java streamJava stream
Java stream
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Io Streams
Io StreamsIo Streams
Io Streams
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Java
JavaJava
Java
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Java Input and Output
Java Input and OutputJava Input and Output
Java Input and Output
 
Md121 streams
Md121 streamsMd121 streams
Md121 streams
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Io streams
Io streamsIo streams
Io streams
 

More from M Vishnuvardhan Reddy

More from M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Scanner class
Scanner classScanner class
Scanner class
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java intro
Java introJava intro
Java intro
 
Java applets
Java appletsJava applets
Java applets
 
Exception handling
Exception handling Exception handling
Exception handling
 
Control structures
Control structuresControl structures
Control structures
 
Constructors
ConstructorsConstructors
Constructors
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Shell sort
Shell sortShell sort
Shell sort
 

Recently uploaded

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

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