SlideShare a Scribd company logo
java.io Package
Sujit Kumar
Zenolocity LLC
Copyright 2012-2024 @
java.io Package
• The java.io package contains a fairly large number
of classes that deal with Java input & output.
Most of the classes consist of:
• Byte streams : that are subclasses of the abstract
classes InputStream or OutputStream
• Character streams : that are subclasses of the
abstract classes Reader and Writer
• The methods for reading & writing data from
byte streams and character streams are similar.
java.io Package (contd…)
• The Reader and Writer classes read and write
16-bit Unicode characters.
• The InputStream and OutputStream classes
read and write 8-bit bytes respectively.
• Derived classes of the above 4 abstract classes
add additional responsibilities using the
Decorator pattern.
How to choose the correct
implementation (derived class)?
•
•
•
•

What is your format: text or binary ?
Do you want random access to a file ?
Are you using objects or non-objects ?
What are your sources and sinks of data (like
sockets, files, strings or array of characters) ?
• Do you need to use filtering techniques like
buffering, checksumming ?
Byte Stream Abstract Classes
InputStream — byte reader

•
•
•
•

read() a single byte or an array of bytes
skip() bytes
mark() and reset() position
close() the stream (always in finally block)

OutputStream —byte writer
• write() a single byte or an array of bytes
• flush() the stream (in case it is buffered)
• close() the stream (always in finally block)
Character Stream Abstract Classes
Reader — character reader

•
•
•
•

read() a single char or into a char array
skip() chars
mark() and reset() position
close() the stream (always in finally block)

Writer — character writer
• write() a single char or from a char array
• flush() the stream (in case it is buffered)
• close() the stream (always in finally block)
Byte Stream Concrete classes
•
•
•
•
•
•
•
•

FileInputStream, FileOutputStream
BufferedInputStream, BufferedOutputStream
ByteArrayInputStream, ByteArrayOutputStream
DataInputStream, DataOutputStream
FilterInputStream, FilterOutputStream
ObjectInputStream, ObjectOutputStream
PipedInputStream, PipedOutputStream
LineNumberInputStream

• PrintStream
Character Stream Concrete classes
•
•
•
•
•
•
•
•

FileReader, FileWriter
BufferedReader, BufferedWriter
CharArrayReader, CharArrayWriter
InputStreamReader, InputStreamWriter
FilterReader, FilterWriter
ObjectReader, ObjectWriter
PipedReader, PipedWriter
StringReader, StringWriter

• LineNumberReader
• PrintWriter
Applying the Decorator Pattern
• Example: Read each line from a file
• BufferedReader => InputStreamReader =>
FileInputStream
• Wrap an InputStreamReader over a
FileInputStream.
• Then, wrap a BufferedReader over the
InputStreamReader.
Applying the Decorator Pattern
(contd…)
• BufferedReader => Reads text from a characterinput stream, buffering characters so as to
provide for the efficient reading of characters,
arrays, and lines.
• FileInputStream => obtains input bytes from a file
in a file system.
• InputStreamReader => bridge from byte streams
to character streams. It reads bytes and decodes
them into characters using a specified charset.
Exception Handling
• The constructors and methods of I/O classes can
throw a checked exception.
• The base class for I/O exceptions is
java.io.IOException. Some methods can throw a
subclass of IOException.
For example, the constructors of FileInputStream, FileOutputStream and
FileReader can throw FileNotFoundException.

• The call to close() should always occur in a finally
block (ensures that the I/O channel is closed
properly even if an exception occurs).

More Related Content

What's hot (19)

PPT
Input output streams
Parthipan Parthi
 
ODP
IO In Java
parag
 
PPT
Character stream classes .52
myrajendra
 
PPTX
L21 io streams
teach4uin
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
PPTX
Java stereams
Jernej Virag
 
PPTX
Javaiostream
Manav Prasad
 
PPTX
File Handling in Java Oop presentation
Azeemaj101
 
PDF
Basic i/o & file handling in java
JayasankarPR2
 
PPTX
Java I/O
DeeptiJava
 
PPT
IO and serialization
backdoor
 
PPTX
Java program file I/O
Nem Sothea
 
PPTX
An Introduction To Python - Files, Part 1
Blue Elephant Consulting
 
PPT
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
PPTX
Input & output
SAIFUR RAHMAN
 
PPTX
30csharp
Sireesh K
 
PDF
Javaiostream
Tien Nguyen
 
PPTX
9 Inputs & Outputs
Deepak Hagadur Bheemaraju
 
PPTX
32sql server
Sireesh K
 
Input output streams
Parthipan Parthi
 
IO In Java
parag
 
Character stream classes .52
myrajendra
 
L21 io streams
teach4uin
 
Java Streams
M Vishnuvardhan Reddy
 
Java stereams
Jernej Virag
 
Javaiostream
Manav Prasad
 
File Handling in Java Oop presentation
Azeemaj101
 
Basic i/o & file handling in java
JayasankarPR2
 
Java I/O
DeeptiJava
 
IO and serialization
backdoor
 
Java program file I/O
Nem Sothea
 
An Introduction To Python - Files, Part 1
Blue Elephant Consulting
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
Input & output
SAIFUR RAHMAN
 
30csharp
Sireesh K
 
Javaiostream
Tien Nguyen
 
9 Inputs & Outputs
Deepak Hagadur Bheemaraju
 
32sql server
Sireesh K
 

Similar to Java input output package (20)

PPT
Iostreams
aptechsravan
 
PPT
Using Input Output
raksharao
 
PPTX
Java I/O
Jayant Dalvi
 
PDF
UNIT4-IO,Generics,String Handling.pdf Notes
SakkaravarthiS1
 
PDF
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
PPTX
Stream In Java.pptx
ssuser9d7049
 
PPT
Java development development Files lectur6.ppt
rafeakrafeak
 
DOC
Web Technology Web Technology Notes Streams Network Principles and SocketsUni...
uthayashangar1
 
PPTX
I/O Streams
Ravi Chythanya
 
PDF
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
akinbhattarai1
 
PPTX
Java Input and Output
Ducat India
 
PPTX
IOStream.pptx
HindAlmisbahi
 
PDF
Java Day-6
People Strategists
 
PPTX
IO Programming.pptx all informatiyon ppt
nandinimakwana22cse
 
PPTX
Input output files in java
Kavitha713564
 
PPTX
Io streams
Elizabeth alexander
 
DOCX
Unit IV Notes.docx
GayathriRHICETCSESTA
 
PDF
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
PDF
Programming language JAVA Input output opearations
2025183005
 
PPS
Files & IO in Java
CIB Egypt
 
Iostreams
aptechsravan
 
Using Input Output
raksharao
 
Java I/O
Jayant Dalvi
 
UNIT4-IO,Generics,String Handling.pdf Notes
SakkaravarthiS1
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Stream In Java.pptx
ssuser9d7049
 
Java development development Files lectur6.ppt
rafeakrafeak
 
Web Technology Web Technology Notes Streams Network Principles and SocketsUni...
uthayashangar1
 
I/O Streams
Ravi Chythanya
 
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
akinbhattarai1
 
Java Input and Output
Ducat India
 
IOStream.pptx
HindAlmisbahi
 
Java Day-6
People Strategists
 
IO Programming.pptx all informatiyon ppt
nandinimakwana22cse
 
Input output files in java
Kavitha713564
 
Unit IV Notes.docx
GayathriRHICETCSESTA
 
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
Programming language JAVA Input output opearations
2025183005
 
Files & IO in Java
CIB Egypt
 
Ad

More from Sujit Kumar (20)

PPTX
Introduction to OOP with java
Sujit Kumar
 
PPTX
SFDC Database Basics
Sujit Kumar
 
PPTX
SFDC Database Security
Sujit Kumar
 
PPTX
SFDC Social Applications
Sujit Kumar
 
PPTX
SFDC Other Platform Features
Sujit Kumar
 
PPTX
SFDC Outbound Integrations
Sujit Kumar
 
PPTX
SFDC Inbound Integrations
Sujit Kumar
 
PPTX
SFDC UI - Advanced Visualforce
Sujit Kumar
 
PPTX
SFDC UI - Introduction to Visualforce
Sujit Kumar
 
PPTX
SFDC Deployments
Sujit Kumar
 
PPTX
SFDC Batch Apex
Sujit Kumar
 
PPTX
SFDC Data Loader
Sujit Kumar
 
PPTX
SFDC Advanced Apex
Sujit Kumar
 
PPTX
SFDC Introduction to Apex
Sujit Kumar
 
PPTX
SFDC Database Additional Features
Sujit Kumar
 
PPTX
Introduction to SalesForce
Sujit Kumar
 
PPTX
More about java strings - Immutability and String Pool
Sujit Kumar
 
PPTX
Hibernate First and Second level caches
Sujit Kumar
 
PPTX
Java equals hashCode Contract
Sujit Kumar
 
PPTX
Java Comparable and Comparator
Sujit Kumar
 
Introduction to OOP with java
Sujit Kumar
 
SFDC Database Basics
Sujit Kumar
 
SFDC Database Security
Sujit Kumar
 
SFDC Social Applications
Sujit Kumar
 
SFDC Other Platform Features
Sujit Kumar
 
SFDC Outbound Integrations
Sujit Kumar
 
SFDC Inbound Integrations
Sujit Kumar
 
SFDC UI - Advanced Visualforce
Sujit Kumar
 
SFDC UI - Introduction to Visualforce
Sujit Kumar
 
SFDC Deployments
Sujit Kumar
 
SFDC Batch Apex
Sujit Kumar
 
SFDC Data Loader
Sujit Kumar
 
SFDC Advanced Apex
Sujit Kumar
 
SFDC Introduction to Apex
Sujit Kumar
 
SFDC Database Additional Features
Sujit Kumar
 
Introduction to SalesForce
Sujit Kumar
 
More about java strings - Immutability and String Pool
Sujit Kumar
 
Hibernate First and Second level caches
Sujit Kumar
 
Java equals hashCode Contract
Sujit Kumar
 
Java Comparable and Comparator
Sujit Kumar
 
Ad

Recently uploaded (20)

PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Python basic programing language for automation
DanialHabibi2
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 

Java input output package

  • 1. java.io Package Sujit Kumar Zenolocity LLC Copyright 2012-2024 @
  • 2. java.io Package • The java.io package contains a fairly large number of classes that deal with Java input & output. Most of the classes consist of: • Byte streams : that are subclasses of the abstract classes InputStream or OutputStream • Character streams : that are subclasses of the abstract classes Reader and Writer • The methods for reading & writing data from byte streams and character streams are similar.
  • 3. java.io Package (contd…) • The Reader and Writer classes read and write 16-bit Unicode characters. • The InputStream and OutputStream classes read and write 8-bit bytes respectively. • Derived classes of the above 4 abstract classes add additional responsibilities using the Decorator pattern.
  • 4. How to choose the correct implementation (derived class)? • • • • What is your format: text or binary ? Do you want random access to a file ? Are you using objects or non-objects ? What are your sources and sinks of data (like sockets, files, strings or array of characters) ? • Do you need to use filtering techniques like buffering, checksumming ?
  • 5. Byte Stream Abstract Classes InputStream — byte reader • • • • read() a single byte or an array of bytes skip() bytes mark() and reset() position close() the stream (always in finally block) OutputStream —byte writer • write() a single byte or an array of bytes • flush() the stream (in case it is buffered) • close() the stream (always in finally block)
  • 6. Character Stream Abstract Classes Reader — character reader • • • • read() a single char or into a char array skip() chars mark() and reset() position close() the stream (always in finally block) Writer — character writer • write() a single char or from a char array • flush() the stream (in case it is buffered) • close() the stream (always in finally block)
  • 7. Byte Stream Concrete classes • • • • • • • • FileInputStream, FileOutputStream BufferedInputStream, BufferedOutputStream ByteArrayInputStream, ByteArrayOutputStream DataInputStream, DataOutputStream FilterInputStream, FilterOutputStream ObjectInputStream, ObjectOutputStream PipedInputStream, PipedOutputStream LineNumberInputStream • PrintStream
  • 8. Character Stream Concrete classes • • • • • • • • FileReader, FileWriter BufferedReader, BufferedWriter CharArrayReader, CharArrayWriter InputStreamReader, InputStreamWriter FilterReader, FilterWriter ObjectReader, ObjectWriter PipedReader, PipedWriter StringReader, StringWriter • LineNumberReader • PrintWriter
  • 9. Applying the Decorator Pattern • Example: Read each line from a file • BufferedReader => InputStreamReader => FileInputStream • Wrap an InputStreamReader over a FileInputStream. • Then, wrap a BufferedReader over the InputStreamReader.
  • 10. Applying the Decorator Pattern (contd…) • BufferedReader => Reads text from a characterinput stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. • FileInputStream => obtains input bytes from a file in a file system. • InputStreamReader => bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset.
  • 11. Exception Handling • The constructors and methods of I/O classes can throw a checked exception. • The base class for I/O exceptions is java.io.IOException. Some methods can throw a subclass of IOException. For example, the constructors of FileInputStream, FileOutputStream and FileReader can throw FileNotFoundException. • The call to close() should always occur in a finally block (ensures that the I/O channel is closed properly even if an exception occurs).