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

IO In Java
IO In JavaIO In Java
IO In Javaparag
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentationAzeemaj101
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
IO and serialization
IO and serializationIO and serialization
IO and serializationbackdoor
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/ONem Sothea
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsDon Bosco BSIT
 
32sql server
32sql server32sql server
32sql serverSireesh K
 

What's hot (19)

Input output streams
Input output streamsInput output streams
Input output streams
 
IO In Java
IO In JavaIO In Java
IO In Java
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java stereams
Java stereamsJava stereams
Java stereams
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Java I/O
Java I/OJava I/O
Java I/O
 
IO and serialization
IO and serializationIO and serialization
IO and serialization
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1An Introduction To Python - Files, Part 1
An Introduction To Python - Files, Part 1
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
Input & output
Input & outputInput & output
Input & output
 
30csharp
30csharp30csharp
30csharp
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
 
32sql server
32sql server32sql server
32sql server
 

Similar to Java input output package

Using Input Output
Using Input OutputUsing Input Output
Using Input Outputraksharao
 
inputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfinputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfhemanth248901
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization Hitesh-Java
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, SerializationPawanMM
 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxSadhilAggarwal
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scannerArif Ullah
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6Berk Soysal
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxnoonoboom
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptxssuser9d7049
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfVithalReddy3
 
Java session14
Java session14Java session14
Java session14Niit Care
 

Similar to Java input output package (20)

Using Input Output
Using Input OutputUsing Input Output
Using Input Output
 
inputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfinputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdf
 
Md121 streams
Md121 streamsMd121 streams
Md121 streams
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptx
 
Java I/O
Java I/OJava I/O
Java I/O
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
Java
JavaJava
Java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
Basic IO
Basic IOBasic IO
Basic IO
 
javaiostream
javaiostreamjavaiostream
javaiostream
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Java session14
Java session14Java session14
Java session14
 

More from Sujit Kumar

Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with javaSujit Kumar
 
SFDC Database Basics
SFDC Database BasicsSFDC Database Basics
SFDC Database BasicsSujit Kumar
 
SFDC Database Security
SFDC Database SecuritySFDC Database Security
SFDC Database SecuritySujit Kumar
 
SFDC Social Applications
SFDC Social ApplicationsSFDC Social Applications
SFDC Social ApplicationsSujit Kumar
 
SFDC Other Platform Features
SFDC Other Platform FeaturesSFDC Other Platform Features
SFDC Other Platform FeaturesSujit Kumar
 
SFDC Outbound Integrations
SFDC Outbound IntegrationsSFDC Outbound Integrations
SFDC Outbound IntegrationsSujit Kumar
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound IntegrationsSujit Kumar
 
SFDC UI - Advanced Visualforce
SFDC UI - Advanced VisualforceSFDC UI - Advanced Visualforce
SFDC UI - Advanced VisualforceSujit Kumar
 
SFDC UI - Introduction to Visualforce
SFDC UI -  Introduction to VisualforceSFDC UI -  Introduction to Visualforce
SFDC UI - Introduction to VisualforceSujit Kumar
 
SFDC Deployments
SFDC DeploymentsSFDC Deployments
SFDC DeploymentsSujit Kumar
 
SFDC Data Loader
SFDC Data LoaderSFDC Data Loader
SFDC Data LoaderSujit Kumar
 
SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex Sujit Kumar
 
SFDC Introduction to Apex
SFDC Introduction to ApexSFDC Introduction to Apex
SFDC Introduction to ApexSujit Kumar
 
SFDC Database Additional Features
SFDC Database Additional FeaturesSFDC Database Additional Features
SFDC Database Additional FeaturesSujit Kumar
 
Introduction to SalesForce
Introduction to SalesForceIntroduction to SalesForce
Introduction to SalesForceSujit Kumar
 
More about java strings - Immutability and String Pool
More about java strings - Immutability and String PoolMore about java strings - Immutability and String Pool
More about java strings - Immutability and String PoolSujit Kumar
 
Hibernate First and Second level caches
Hibernate First and Second level cachesHibernate First and Second level caches
Hibernate First and Second level cachesSujit Kumar
 
Java equals hashCode Contract
Java equals hashCode ContractJava equals hashCode Contract
Java equals hashCode ContractSujit Kumar
 
Java Comparable and Comparator
Java Comparable and ComparatorJava Comparable and Comparator
Java Comparable and ComparatorSujit Kumar
 

More from Sujit Kumar (20)

Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
SFDC Database Basics
SFDC Database BasicsSFDC Database Basics
SFDC Database Basics
 
SFDC Database Security
SFDC Database SecuritySFDC Database Security
SFDC Database Security
 
SFDC Social Applications
SFDC Social ApplicationsSFDC Social Applications
SFDC Social Applications
 
SFDC Other Platform Features
SFDC Other Platform FeaturesSFDC Other Platform Features
SFDC Other Platform Features
 
SFDC Outbound Integrations
SFDC Outbound IntegrationsSFDC Outbound Integrations
SFDC Outbound Integrations
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound Integrations
 
SFDC UI - Advanced Visualforce
SFDC UI - Advanced VisualforceSFDC UI - Advanced Visualforce
SFDC UI - Advanced Visualforce
 
SFDC UI - Introduction to Visualforce
SFDC UI -  Introduction to VisualforceSFDC UI -  Introduction to Visualforce
SFDC UI - Introduction to Visualforce
 
SFDC Deployments
SFDC DeploymentsSFDC Deployments
SFDC Deployments
 
SFDC Batch Apex
SFDC Batch ApexSFDC Batch Apex
SFDC Batch Apex
 
SFDC Data Loader
SFDC Data LoaderSFDC Data Loader
SFDC Data Loader
 
SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex
 
SFDC Introduction to Apex
SFDC Introduction to ApexSFDC Introduction to Apex
SFDC Introduction to Apex
 
SFDC Database Additional Features
SFDC Database Additional FeaturesSFDC Database Additional Features
SFDC Database Additional Features
 
Introduction to SalesForce
Introduction to SalesForceIntroduction to SalesForce
Introduction to SalesForce
 
More about java strings - Immutability and String Pool
More about java strings - Immutability and String PoolMore about java strings - Immutability and String Pool
More about java strings - Immutability and String Pool
 
Hibernate First and Second level caches
Hibernate First and Second level cachesHibernate First and Second level caches
Hibernate First and Second level caches
 
Java equals hashCode Contract
Java equals hashCode ContractJava equals hashCode Contract
Java equals hashCode Contract
 
Java Comparable and Comparator
Java Comparable and ComparatorJava Comparable and Comparator
Java Comparable and Comparator
 

Recently uploaded

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

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).