SlideShare a Scribd company logo
Java
I/O
File
• Long-term storage of large amounts of data
• Persistent data exists a5er termina6on of program
• Files stored on secondary storage devices
– Magne6c disks
– Op6cal disks
– Magne6c tapes
• Sequen6al and random access files
2
File Class
• Provides useful informa6on about a file or directory
• Does not open files or process files
• To obtain or manipulate path, 6me, date,
permissions etc
• Constructor
– File(String directoryPath)
– File(String directoryPath, String fileName)
– File(File dirObj, String fileName)
• Example: FileDemo.java
3
Directory Class
• Directories are also files
• Contains list of files and directories
• For DirectoryisDirectory() returns true
String[] list()
– returns an array of strings that gives the files and
directories contained
File[] listFiles()
– Returns array of File objects
• Example: DirectoryDemo.java
4
Stream Classes
• Java views a File as a stream of bytes.
– File ends with end-of-file marker or a specific byte number
– File as a stream of bytes associated with an object.
– Java also associates streams with devices
• System.in, System.out, and System.err
– Streams can be redirected
• Stream is an abstrac6on that either produces or
consumes informa6on
5
Stream Classes
• Java’s stream-based I/O is built upon four
abstract classes.
– InputStream, OutputStream (for byte streams)
– Reader, Writer (for character streams)
• They form separate hierarchies
• Use the character stream classes when working
with characters or strings
• Use the byte stream classes when working with
bytes or other binary objects
6
Byte Stream Classes
• Byte-Stream classes are topped by InputStream and
OutputStream classes
• InputStream is an abstract class that defines Java’s
model of streaming byte input.
int available() void close() int read()
int read(byte buff[]) int read(byte buff[], int off, int num)
• OutputStream is an abstract class that defines Java’s
model of streaming byte output.
void flush() void close() void write(int b)
void write(byte buff[]) void write(byte buff[], int off, int num)
7
FileInputStream
• FileInputStream class creates an InputStream that
you can use to read bytes from a file
• Constructors
– FileInputStream(String filePath)
– FileInputStream(File fileObj)
• Example: FileInputStreamDemo.java
8
FileOutputStream
• FileOutputStream class creates an OutputStream
that you can use to write bytes to a file
• Constructors
– FileOutputStream(String filePath)
– FileOutputStream(File fileObj)
– FileOutputStream(String path, boolean append)
– FileOutputStream(File obj, boolean append)
• Example: FileOutputStreamDemo.java, FileCopyDemo.java
9
Character Streams
• Character Stream classes are topped by Reader and
Writer class
• Reader is an abstract class that defines Java’s model
of streaming character input
void close() int read() int read(char buff[])
int read(char buff[], int off, int num)
• Writer is an abstract class that defines Java’s model
of streaming character output
void flush() void close() void write(int ch)
void write(char buff[]) void write(char buff[], int off, int num)
void write(String s) void write(String s, int off, int num) 10
FileReader
• FileReader class creates a Reader that you can use to
read the contents of a file
• Constructors
– FileReader(String filePath)
– FileReader(File fileObj)
• Example: FileReaderDemo.java
11
FileWriter
• FileWriter class creates a Writer that you can use to
write to a file
• Constructors
– FileWriter(String filePath)
– FileWriter(File fileObj)
– FileWriter(String path, boolean append)
– FileWriter(File obj, boolean append)
• Example: FileWriterDemo.java
12
BufferedReader
• BufferedReader is a Reader that buffers input
• It improves performance by reducing the number of
6mes data us actually physically read from the input
stream
• Constructors
– BufferedReader(Reader reader)
– BufferedReader(Reader reader, int buffSize)
• Example: BufferedReaderDemo.java
13
BufferedWriter
• BufferedWriter is a Writer that buffers output
• It improves performance by reducing the number of
6mes data actually physically wricen to the output
stream
• Constructors
– BufferedWriter(Writer writer)
– BufferedWriter(Writer writer, int buffSize)
• Example: BufferedWriterDemo.java
14
Serializa6on
• Serializa6on is the process of wri6ng the state of an
object to a byte stream
– This is useful when you want to save the state of your
program to a persistent storage such as file
– Later these objects can be restored by using the process of
deserializa6on
• Serializa6on can be achieved by implemen6ng
Serializable interface
15
Object(Input/Output)Stream
• ObjectInputStream class extends the InputStream
class
• It is responsible for reading objects from a stream
• ObjectOutputStream class extends the
OutputStream class
• It is responsible for wri6ng objects to a stream
• Example: ObjectSerializaJonDemo.java
16
Self Study
Data(Input/Output)Stream
• DataInputStream & DataOutputStream enable to
write or read primi6ve data to or from a stream
• They implement the DataOutput & DataInput
interfaces respec6vely
• Constructors
– DataOutputStream(OutputStream os)
– DataInputStream(InputStream is)
• Example: DataIODemo.java
18
Console
• It is used to read and write to the console
• It supplies no constructor. A Console object is
obtained by calling System.console()
• Important Methods
– prinf,
– readLine
– readPassword
• Example: ConsoleDemo.java
19
RandomAccessFile
20

More Related Content

Similar to Programming language JAVA Input output opearations (20)

Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Java
JavaJava
Java
Dhruv Sabalpara
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
People Strategists
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
siragezeynu
 
Jstreams
JstreamsJstreams
Jstreams
Shehrevar Davierwala
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
NilaNila16
 
Javaio
JavaioJavaio
Javaio
Jaya Jeswani
 
Javaio
JavaioJavaio
Javaio
Jaya Jeswani
 
chapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and streamchapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and stream
amarehope21
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Java stream
Java streamJava stream
Java stream
Arati Gadgil
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Azeemaj101
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Io streams
Io streamsIo streams
Io streams
Elizabeth alexander
 
0301502 ADVANCED JAVA_UNIT_1.pdf
0301502 ADVANCED JAVA_UNIT_1.pdf0301502 ADVANCED JAVA_UNIT_1.pdf
0301502 ADVANCED JAVA_UNIT_1.pdf
jaydesai523137
 

Recently uploaded (20)

A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Research_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptxResearch_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptx
niranjancse
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptxThe first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant ConversionStructural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdfHow Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Research_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptxResearch_Sensitization_&_Innovative_Project_Development.pptx
Research_Sensitization_&_Innovative_Project_Development.pptx
niranjancse
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptxThe first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
Ad

Programming language JAVA Input output opearations

  • 2. File • Long-term storage of large amounts of data • Persistent data exists a5er termina6on of program • Files stored on secondary storage devices – Magne6c disks – Op6cal disks – Magne6c tapes • Sequen6al and random access files 2
  • 3. File Class • Provides useful informa6on about a file or directory • Does not open files or process files • To obtain or manipulate path, 6me, date, permissions etc • Constructor – File(String directoryPath) – File(String directoryPath, String fileName) – File(File dirObj, String fileName) • Example: FileDemo.java 3
  • 4. Directory Class • Directories are also files • Contains list of files and directories • For DirectoryisDirectory() returns true String[] list() – returns an array of strings that gives the files and directories contained File[] listFiles() – Returns array of File objects • Example: DirectoryDemo.java 4
  • 5. Stream Classes • Java views a File as a stream of bytes. – File ends with end-of-file marker or a specific byte number – File as a stream of bytes associated with an object. – Java also associates streams with devices • System.in, System.out, and System.err – Streams can be redirected • Stream is an abstrac6on that either produces or consumes informa6on 5
  • 6. Stream Classes • Java’s stream-based I/O is built upon four abstract classes. – InputStream, OutputStream (for byte streams) – Reader, Writer (for character streams) • They form separate hierarchies • Use the character stream classes when working with characters or strings • Use the byte stream classes when working with bytes or other binary objects 6
  • 7. Byte Stream Classes • Byte-Stream classes are topped by InputStream and OutputStream classes • InputStream is an abstract class that defines Java’s model of streaming byte input. int available() void close() int read() int read(byte buff[]) int read(byte buff[], int off, int num) • OutputStream is an abstract class that defines Java’s model of streaming byte output. void flush() void close() void write(int b) void write(byte buff[]) void write(byte buff[], int off, int num) 7
  • 8. FileInputStream • FileInputStream class creates an InputStream that you can use to read bytes from a file • Constructors – FileInputStream(String filePath) – FileInputStream(File fileObj) • Example: FileInputStreamDemo.java 8
  • 9. FileOutputStream • FileOutputStream class creates an OutputStream that you can use to write bytes to a file • Constructors – FileOutputStream(String filePath) – FileOutputStream(File fileObj) – FileOutputStream(String path, boolean append) – FileOutputStream(File obj, boolean append) • Example: FileOutputStreamDemo.java, FileCopyDemo.java 9
  • 10. Character Streams • Character Stream classes are topped by Reader and Writer class • Reader is an abstract class that defines Java’s model of streaming character input void close() int read() int read(char buff[]) int read(char buff[], int off, int num) • Writer is an abstract class that defines Java’s model of streaming character output void flush() void close() void write(int ch) void write(char buff[]) void write(char buff[], int off, int num) void write(String s) void write(String s, int off, int num) 10
  • 11. FileReader • FileReader class creates a Reader that you can use to read the contents of a file • Constructors – FileReader(String filePath) – FileReader(File fileObj) • Example: FileReaderDemo.java 11
  • 12. FileWriter • FileWriter class creates a Writer that you can use to write to a file • Constructors – FileWriter(String filePath) – FileWriter(File fileObj) – FileWriter(String path, boolean append) – FileWriter(File obj, boolean append) • Example: FileWriterDemo.java 12
  • 13. BufferedReader • BufferedReader is a Reader that buffers input • It improves performance by reducing the number of 6mes data us actually physically read from the input stream • Constructors – BufferedReader(Reader reader) – BufferedReader(Reader reader, int buffSize) • Example: BufferedReaderDemo.java 13
  • 14. BufferedWriter • BufferedWriter is a Writer that buffers output • It improves performance by reducing the number of 6mes data actually physically wricen to the output stream • Constructors – BufferedWriter(Writer writer) – BufferedWriter(Writer writer, int buffSize) • Example: BufferedWriterDemo.java 14
  • 15. Serializa6on • Serializa6on is the process of wri6ng the state of an object to a byte stream – This is useful when you want to save the state of your program to a persistent storage such as file – Later these objects can be restored by using the process of deserializa6on • Serializa6on can be achieved by implemen6ng Serializable interface 15
  • 16. Object(Input/Output)Stream • ObjectInputStream class extends the InputStream class • It is responsible for reading objects from a stream • ObjectOutputStream class extends the OutputStream class • It is responsible for wri6ng objects to a stream • Example: ObjectSerializaJonDemo.java 16
  • 18. Data(Input/Output)Stream • DataInputStream & DataOutputStream enable to write or read primi6ve data to or from a stream • They implement the DataOutput & DataInput interfaces respec6vely • Constructors – DataOutputStream(OutputStream os) – DataInputStream(InputStream is) • Example: DataIODemo.java 18
  • 19. Console • It is used to read and write to the console • It supplies no constructor. A Console object is obtained by calling System.console() • Important Methods – prinf, – readLine – readPassword • Example: ConsoleDemo.java 19