SlideShare una empresa de Scribd logo
1 de 16
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Java IO
Java IOJava IO
Java IO
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
JAVA PROGRAMMING
JAVA PROGRAMMING JAVA PROGRAMMING
JAVA PROGRAMMING
 
Interface
InterfaceInterface
Interface
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
for loop in java
for loop in java for loop in java
for loop in java
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Files in java
Files in javaFiles in java
Files in java
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 

Similar a Io streams

Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptxssuser9d7049
 
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 NotesSakkaravarthiS1
 
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 & Outputphanleson
 
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.pptxRathanMB
 
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/OWebStackAcademy
 
Java Input and Output
Java Input and OutputJava Input and Output
Java Input and OutputDucat India
 
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
 
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
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 

Similar a 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
 

Más de Elizabeth alexander (10)

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
 

Último

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Último (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 

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