SlideShare a Scribd company logo
IO STREAMS
Elizabeth Alexander
Hindustan University
Input/Output in Java
● Programs read inputs from data sources (e.g., keyboard, file, network, memory
buffer, or another program) and write outputs to data sinks (e.g., display console,
file, network, memory buffer, or another program).
● Java I/O (Input and Output) is used to process the input and produce the output.
● Java uses the concept of stream to make I/O operation fast. The java.io package
contains all the classes required for input and output operations.
● A stream is a sequential and contiguous one-way flow of data (just like water or oil
flows through the pipe).
● Java does not differentiate between the various types of data sources or sinks
(e.g., file or network) in stream I/O. They are all treated as a sequential flow of
data.
Java I/O (Input and Output)
● The Java program receives data from a source by opening an input stream, and
sends data to a sink by opening an output stream. All Java I/O streams are one-
way (except the RandomAccessFile).
● If your program needs to perform both input and output, you have to open two
streams - an input stream and an output stream.
● Stream I/O operations involve three steps:
○ Open an input/output stream associated with a physical device (e.g., file,
network, console/keyboard), by constructing an appropriate I/O stream
instance.
○ Read from the opened input stream until "end-of-stream" encountered, or
write to the opened output stream (and optionally flush the buffered output).
○ Close the input/output stream.
Stream
● A stream can be defined as a sequence of data. There are two kinds of Streams −
○ InPutStream − The InputStream is used to read data from a source.
○ OutPutStream − The OutputStream is used for writing data to a destination.
● Java defines two types of streams. They are,
○ Byte Stream : It provides a convenient means for handling input and output
of byte.
○ Character Stream : It provides a convenient means for handling input and
output of characters.
Byte Streams
● Byte stream is defined by using two abstract class at the top of hierarchy, they
are InputStream and OutputStream.
Some important Byte stream classes.
Byte Streams Cont...
● These classes define several key methods. Two most important are
○ read() : reads byte of data.
○ write() : Writes byte of data
Character Streams.
● Character stream is also defined by using two abstract class at the top of
hierarchy, they are Reader and Writer.
Character Streams Cont...
● These two abstract classes have several concrete classes that handle unicode
character.
Some important Character stream classes.
Reading Console Input
● Example: We use the object of BufferedReader class to take inputs from the
keyboard.
Reading Characters
● Example : read() method is used with BufferedReader object to read characters.
As this function returns integer type value has we need to use typecasting to
convert it into char type.
class CharRead
{
public static void main( String args[])
{
BufferedReader br = new Bufferedreader(new InputstreamReader(System.in));
char c = (char)br.read(); //Reading character
}
}
Reading Strings
● To read string we have to use readLine() function with BufferedReader class's
object.
Program to take String input from Keyboard in Java
import java.io.*;
class MyInput
{
public static void main(String[] args)
{
String text;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
text = br.readLine(); //Reading String
System.out.println(text);
}
}
Reading and Writing on Files
● You can read files using these classes:
○ FileReader for text files
○ FileInputStream for binary files and text files that contain 'weird' characters.
○ FileInputStream : Objects can be created using the keyword new and there
are several types of constructors available.
■ InputStream f = new FileInputStream("C:/java/hello");
● constructor takes a file name as a string to create an input stream
object to read the file
OR
■ File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
● constructor takes a file object to create an input stream object to
read the file. First we create a file object using File() method
Writing on Files
● To write a text file in Java, use FileWriter instead of FileReader, and
BufferedOutputWriter instead of BufferedOutputReader
● FileOutputStream is used to create a file and write data into it. The stream would
create a file, if it doesn't already exist, before opening it for output.
● Here are two constructors which can be used to create a FileOutputStream
object.
○ OutputStream f = new FileOutputStream("C:/java/hello")
■ constructor takes a file name as a string to create an input stream object
to write the file
or
○ File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
■ constructor takes a file object to create an output stream object to write
the file. First, we create a file object using File() method
InputStreamReader
● The java.io.InputStreamReader is a bridge from byte streams to character
streams.
● It reads bytes and decodes them into characters using a specified charset.
● The charset that it uses may be specified by name or may be given explicitly, or
the platform’s default charset may be accepted.
OutputStreamWriter
● The Java.io.OutputStreamWriter class is a bridge from character streams to
byte streams. Characters written to it are encoded into bytes using a specified
charset.
Stream Chaining
● Java uses the concept of "chaining" streams to allow the creation of complex I/O
processing.
● This means that an instance of one Stream is passed as a parameter to the
constructor of another

More Related Content

What's hot

Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Java I/O
Java I/OJava I/O
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
Rohit Jain
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
14 file handling
14 file handling14 file handling
14 file handling
APU
 
Files in java
Files in javaFiles in java
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 

What's hot (20)

Input output streams
Input output streamsInput output streams
Input output streams
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java IO
Java IOJava IO
Java IO
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Java swing
Java swingJava swing
Java swing
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
14 file handling
14 file handling14 file handling
14 file handling
 
Files in java
Files in javaFiles in java
Files in java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Applets in java
Applets in javaApplets in java
Applets in java
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 

Similar to Io streams

Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
ssuser9d7049
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
Manav Prasad
 
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
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
cs19club
 
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
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
GayathriRHICETCSESTA
 
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
 
Md121 streams
Md121 streamsMd121 streams
Md121 streams
Rakesh Madugula
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
WebStackAcademy
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
Java Input and Output
Java Input and OutputJava Input and Output
Java Input and Output
Ducat India
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
Tien Nguyen
 
javaiostream
javaiostreamjavaiostream
javaiostream
Arjun Shanka
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
siragezeynu
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
Berk Soysal
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
noonoboom
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
Debasish Pratihari
 
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
Don Bosco BSIT
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 

Similar to Io streams (20)

Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
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
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
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
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
 
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
 
Md121 streams
Md121 streamsMd121 streams
Md121 streams
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Java Input and Output
Java Input and OutputJava Input and Output
Java Input and Output
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
javaiostream
javaiostreamjavaiostream
javaiostream
 
Java I/O
Java I/OJava I/O
Java I/O
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
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
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
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
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 

More from Elizabeth alexander

Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Java applet
Java appletJava applet
Java applet
Elizabeth alexander
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Elizabeth alexander
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Quantitative Aptitude- Number System
Quantitative Aptitude- Number SystemQuantitative Aptitude- Number System
Quantitative Aptitude- Number System
Elizabeth alexander
 
Java Programming
Java ProgrammingJava Programming
Java Programming
Elizabeth alexander
 

More from Elizabeth alexander (11)

Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Java applet
Java appletJava applet
Java applet
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Quantitative Aptitude- Number System
Quantitative Aptitude- Number SystemQuantitative Aptitude- Number System
Quantitative Aptitude- Number System
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 

Recently uploaded

A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 

Recently uploaded (20)

A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 

Io streams

  • 2. Input/Output in Java ● Programs read inputs from data sources (e.g., keyboard, file, network, memory buffer, or another program) and write outputs to data sinks (e.g., display console, file, network, memory buffer, or another program). ● Java I/O (Input and Output) is used to process the input and produce the output. ● Java uses the concept of stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations. ● A stream is a sequential and contiguous one-way flow of data (just like water or oil flows through the pipe). ● Java does not differentiate between the various types of data sources or sinks (e.g., file or network) in stream I/O. They are all treated as a sequential flow of data.
  • 3. Java I/O (Input and Output) ● The Java program receives data from a source by opening an input stream, and sends data to a sink by opening an output stream. All Java I/O streams are one- way (except the RandomAccessFile). ● If your program needs to perform both input and output, you have to open two streams - an input stream and an output stream. ● Stream I/O operations involve three steps: ○ Open an input/output stream associated with a physical device (e.g., file, network, console/keyboard), by constructing an appropriate I/O stream instance. ○ Read from the opened input stream until "end-of-stream" encountered, or write to the opened output stream (and optionally flush the buffered output). ○ Close the input/output stream.
  • 4. Stream ● A stream can be defined as a sequence of data. There are two kinds of Streams − ○ InPutStream − The InputStream is used to read data from a source. ○ OutPutStream − The OutputStream is used for writing data to a destination. ● Java defines two types of streams. They are, ○ Byte Stream : It provides a convenient means for handling input and output of byte. ○ Character Stream : It provides a convenient means for handling input and output of characters.
  • 5. Byte Streams ● Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and OutputStream.
  • 6. Some important Byte stream classes.
  • 7. Byte Streams Cont... ● These classes define several key methods. Two most important are ○ read() : reads byte of data. ○ write() : Writes byte of data Character Streams. ● Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader and Writer.
  • 8. Character Streams Cont... ● These two abstract classes have several concrete classes that handle unicode character. Some important Character stream classes.
  • 9. Reading Console Input ● Example: We use the object of BufferedReader class to take inputs from the keyboard.
  • 10. Reading Characters ● Example : read() method is used with BufferedReader object to read characters. As this function returns integer type value has we need to use typecasting to convert it into char type. class CharRead { public static void main( String args[]) { BufferedReader br = new Bufferedreader(new InputstreamReader(System.in)); char c = (char)br.read(); //Reading character } }
  • 11. Reading Strings ● To read string we have to use readLine() function with BufferedReader class's object. Program to take String input from Keyboard in Java import java.io.*; class MyInput { public static void main(String[] args) { String text; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); text = br.readLine(); //Reading String System.out.println(text); } }
  • 12. Reading and Writing on Files ● You can read files using these classes: ○ FileReader for text files ○ FileInputStream for binary files and text files that contain 'weird' characters. ○ FileInputStream : Objects can be created using the keyword new and there are several types of constructors available. ■ InputStream f = new FileInputStream("C:/java/hello"); ● constructor takes a file name as a string to create an input stream object to read the file OR ■ File f = new File("C:/java/hello"); InputStream f = new FileInputStream(f); ● constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method
  • 13. Writing on Files ● To write a text file in Java, use FileWriter instead of FileReader, and BufferedOutputWriter instead of BufferedOutputReader ● FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output. ● Here are two constructors which can be used to create a FileOutputStream object. ○ OutputStream f = new FileOutputStream("C:/java/hello") ■ constructor takes a file name as a string to create an input stream object to write the file or ○ File f = new File("C:/java/hello"); OutputStream f = new FileOutputStream(f); ■ constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method
  • 14. InputStreamReader ● The java.io.InputStreamReader is a bridge from byte streams to character streams. ● It reads bytes and decodes them into characters using a specified charset. ● The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.
  • 15. OutputStreamWriter ● The Java.io.OutputStreamWriter class is a bridge from character streams to byte streams. Characters written to it are encoded into bytes using a specified charset.
  • 16. Stream Chaining ● Java uses the concept of "chaining" streams to allow the creation of complex I/O processing. ● This means that an instance of one Stream is passed as a parameter to the constructor of another