ๅฐŠๆ•ฌ็š„ ๅพฎไฟกๆฑ‡็Ž‡๏ผš1ๅ†† โ‰ˆ 0.046166 ๅ…ƒ ๆ”ฏไป˜ๅฎๆฑ‡็Ž‡๏ผš1ๅ†† โ‰ˆ 0.046257ๅ…ƒ [้€€ๅ‡บ็™ปๅฝ•]
SlideShare a Scribd company logo
JAVA
I/O Streams
Prepared by
Miss. Arati A. Gadgil
2
I/O Streams
An I/O Stream represents an input source or an output destination. A
stream can represent many different kinds of sources and destinations,
including disk files, devices, other programs, and memory arrays.
Streams support many different kinds of data, including simple bytes,
primitive data types, localized characters, and objects. Some streams
simply pass on data; others manipulate and transform the data in useful
ways.
A stream is a sequence of data.
A program uses an input stream to read data from a source, one item at a
time.
A program uses an output stream to write data to a destination, one item
at time
3
The basic stream classes are defined in the package โ€œjava.ioโ€.
๏ƒ˜A Java program is reasonably well served by its default state when
execution begins. Three streams are setup and ready to go. There are two
output streams, identified by the objects System.out and System.err, and
one input stream, identified by the object System.in. These objects are
defined as public data fields in the System class of the java.lang package
๏ƒ˜The err and out objects are instances of the PrintStream class and the
in object is an instance of the InputStream class.
Java defines two types of streams.
๏ƒ˜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. Character stream uses Unicode and therefore
can be internationalized.
4
5
Byte Stream Classes
Byte stream is defined by using two abstract class at the top of hierarchy,
they are InputStream and OutputStream
BufferedInputStream :Used for Buffered Input Stream.
BufferedOutputStream: Used for Buffered Output Stream.
DataInputStream: Contains method for reading java standard datatype
DataOutputStream: An output stream that contain method for writing
java standard data type
6
FileInputStreamInput: stream that reads from a file
FileOutputStreamOutput: stream that write to a file.
InputStream: Abstract class that describe stream input.
OutputStream: Abstract class that describe stream output.
PrintStreamOutput: Stream that contain print() and println() method
Methods
read() : reads byte of data.
write() : Writes byte of data.
7
Character Stream Classes
Character stream is also defined by using two abstract class at the top of
hierarchy, they are Reader and Writer.
Charcter stream classes
BufferedReaderHandles buffered input stream.
BufferedWriterHandles buffered output stream.
FileReaderInput stream that reads from file.
FileWriterOutput stream that writes to file.
InputStreamReaderInput stream that translate byte to character
8
OutputStreamReaderOutput stream that translate character to byte.
PrintWriterOutput Stream that contain print() and println() method.
ReaderAbstract class that define character stream input
WriterAbstract class that define character stream output
9
We use the object of BufferedReader class to take inputs from the
keyboard.
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.
To read string we have to use readLine() function with BufferedReader
class's object.
10
Scanner
constructors
Scanner(File source)
Constructs a new Scanner that produces values scanned from
the specified file.
Scanner(InputStream source)
Constructs a new Scanner that produces values scanned from
the specified input stream.
Scanner(Readable source)
Constructs a new Scanner that produces values scanned from
the specified source.
Scanner(String source)
Constructs a new Scanner that produces values scanned from
the specified string.
11
Scanner will read a line of input from its source
Scanner sc = new Scanner (System.in);
int i = sc.nextInt();
System.out.println("You entered" + i);
This example reads a single int from System.in and outputs it to
System.out. It does not check that the user actually entered an int.
12
Next Methods
String next() Finds and returns the next complete token from this
scanner.
boolean nextBoolean() Scans the next token of the input into a boolean
value and returns that value.
byte nextByte() Scans the next token of the input as a byte.
double nextDouble() Scans the next token of the input as a double.
float nextFloat() Scans the next token of the input as a float.
int nextInt() Scans the next token of the input as an int.
String nextLine() Advances this scanner past the current line and returns
the input that was skipped.
long nextLong() Scans the next token of the input as a long.
short nextShort() Scans the next token of the input as a short.
13
hasNext methods
boolean hasNext()
Returns true if this scanner has another token in its input.
boolean hasNextBoolean()
Returns true if the next token in this scanner's input can be interpreted as
a boolean value using a case insensitive pattern created from the string
"true|false".
boolean hasNextByte()
Returns true if the next token in this scanner's input can be interpreted as
a byte value in the default radix using the nextByte() method.
boolean hasNextDouble()
Returns true if the next token in this scanner's input can be interpreted as
a double value using the nextDouble() method.
boolean hasNextFloat()
Returns true if the next token in this scanner's input can be interpreted as
a float value using the nextFloat() method.
14
boolean hasNextInt()
Returns true if the next token in this scanner's input can be
interpreted as an int value in the default radix using the nextInt()
method.
boolean hasNextLine()
Returns true if there is another line in the input of this scanner.
boolean hasNextLong()
Returns true if the next token in this scanner's input can be
interpreted as a long value in the default radix using the
nextLong() method.
boolean hasNextShort()
Returns true if the next token in this scanner's input can be
interpreted as a short value in the default radix using the
nextShort() method.
15
RandomAccessFile
The RandomAccessFile class in the Java IO API allows you to move
around a file and read from it or write to it. We can replace existing parts
of a file too. This is not possible with the FileInputStream or
FileOutputStream.
RandomAccessFile file = new
RandomAccessFile("c:datafile.txt", "rw");
To read or write at a specific location in a RandomAccessFile you must
first position the file pointer at the location to read or write. This is done
using the seek() method. The current position of the file pointer can be
obtained by calling the getFilePointer() method.
RandomAccessFile file = new
RandomAccessFile("c:datafile.txt", "rw");
file.seek(200);
long pointer = file.getFilePointer(); file.close();
16
Reading from a RandomAccessFile
Reading from a RandomAccessFile is done using one of it many read()
methods.
RandomAccessFile file = new
RandomAccessFile("c:datafile.txt", "rw");
int aByte = file.read();
file.close();
The read() method reads the byte located a the position in the file
currently pointed to by the file pointer in theRandomAccessFile instance.
17
Writing to a RandomAccessFile
Writing to a RandomAccessFile can be done using one it its
many write() methods.
RandomAccessFile file = new
RandomAccessFile("c:datafile.txt", "rw");
file.write("Hello World".getBytes());
file.close();
Just like with the read() method the write() method advances the file
pointer after being called. That way you don't have to constantly move
the file pointer to write data to a new location in the file.
Thank You
18

More Related Content

What's hot

Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Hiranya Jayathilaka
ย 
Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
ย 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
ย 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
ย 
Java I/O
Java I/OJava I/O
Java I/O
Jussi Pohjolainen
ย 
32.java input-output
32.java input-output32.java input-output
32.java input-output
santosh mishra
ย 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
ย 
Files in java
Files in javaFiles in java
Files in java
Muthukumaran Subramanian
ย 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Marcello Thiry
ย 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
myrajendra
ย 
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
ย 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
myrajendra
ย 
Java file
Java fileJava file
Java file
sonnetdp
ย 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
Tien Nguyen
ย 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
sharma230399
ย 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
myrajendra
ย 
IO and serialization
IO and serializationIO and serialization
IO and serialization
backdoor
ย 
Io streams
Io streamsIo streams
Io streams
Elizabeth alexander
ย 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
ย 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
PRN USM
ย 

What's hot (20)

Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
ย 
Input output streams
Input output streamsInput output streams
Input output streams
ย 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
ย 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
ย 
Java I/O
Java I/OJava I/O
Java I/O
ย 
32.java input-output
32.java input-output32.java input-output
32.java input-output
ย 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
ย 
Files in java
Files in javaFiles in java
Files in java
ย 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
ย 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
ย 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
ย 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
ย 
Java file
Java fileJava file
Java file
ย 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
ย 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
ย 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
ย 
IO and serialization
IO and serializationIO and serialization
IO and serialization
ย 
Io streams
Io streamsIo streams
Io streams
ย 
Java I/O
Java I/OJava I/O
Java I/O
ย 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
ย 

Similar to Java stream

Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
cs19club
ย 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
ย 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
Bharat17485
ย 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
GayathriRHICETCSESTA
ย 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
ssuser9d7049
ย 
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
ย 
ppt on scanner class
ppt on scanner classppt on scanner class
ppt on scanner class
deepsxn
ย 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
ย 
Java Input and Output
Java Input and OutputJava Input and Output
Java Input and Output
Ducat India
ย 
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
ย 
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
ย 
Io Streams
Io StreamsIo Streams
Io Streams
phanleson
ย 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
ย 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
Berk Soysal
ย 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
Arif Ullah
ย 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
Kuntal Bhowmick
ย 
Nhap xuat trong java
Nhap xuat trong javaNhap xuat trong java
Nhap xuat trong java
tuhn
ย 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
kanchanmahajan23
ย 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
kanchanmahajan23
ย 
Chapter 6
Chapter 6Chapter 6
Chapter 6
siragezeynu
ย 

Similar to Java stream (20)

Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
ย 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
ย 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
ย 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
ย 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
ย 
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
ย 
ppt on scanner class
ppt on scanner classppt on scanner class
ppt on scanner class
ย 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
ย 
Java Input and Output
Java Input and OutputJava Input and Output
Java Input and Output
ย 
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
ย 
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
ย 
Io Streams
Io StreamsIo Streams
Io Streams
ย 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
ย 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
ย 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
ย 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
ย 
Nhap xuat trong java
Nhap xuat trong javaNhap xuat trong java
Nhap xuat trong java
ย 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
ย 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
ย 
Chapter 6
Chapter 6Chapter 6
Chapter 6
ย 

More from Arati Gadgil

Java adapter
Java adapterJava adapter
Java adapter
Arati Gadgil
ย 
Java swing
Java swingJava swing
Java swing
Arati Gadgil
ย 
Java applet
Java appletJava applet
Java applet
Arati Gadgil
ย 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
Arati Gadgil
ย 
Java awt
Java awtJava awt
Java awt
Arati Gadgil
ย 
Java thread
Java threadJava thread
Java thread
Arati Gadgil
ย 
Java networking
Java networkingJava networking
Java networking
Arati Gadgil
ย 
Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
ย 
Java package
Java packageJava package
Java package
Arati Gadgil
ย 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
ย 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
ย 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
Arati Gadgil
ย 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
ย 
Java collection
Java collectionJava collection
Java collection
Arati Gadgil
ย 
Java class
Java classJava class
Java class
Arati Gadgil
ย 
Java basic
Java basicJava basic
Java basic
Arati Gadgil
ย 

More from Arati Gadgil (16)

Java adapter
Java adapterJava adapter
Java adapter
ย 
Java swing
Java swingJava swing
Java swing
ย 
Java applet
Java appletJava applet
Java applet
ย 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
ย 
Java awt
Java awtJava awt
Java awt
ย 
Java thread
Java threadJava thread
Java thread
ย 
Java networking
Java networkingJava networking
Java networking
ย 
Java jdbc
Java jdbcJava jdbc
Java jdbc
ย 
Java package
Java packageJava package
Java package
ย 
Java interface
Java interfaceJava interface
Java interface
ย 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
ย 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
ย 
Java exception
Java exception Java exception
Java exception
ย 
Java collection
Java collectionJava collection
Java collection
ย 
Java class
Java classJava class
Java class
ย 
Java basic
Java basicJava basic
Java basic
ย 

Recently uploaded

Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
ย 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
Kalna College
ย 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
ย 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
shabeluno
ย 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
Celine George
ย 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
ย 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
biruktesfaye27
ย 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
Friends of African Village Libraries
ย 
How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...
Infosec
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
ย 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Catherine Dela Cruz
ย 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
whatchangedhowreflec
ย 
Post init hook in the odoo 17 ERP Module
Post init hook in the  odoo 17 ERP ModulePost init hook in the  odoo 17 ERP Module
Post init hook in the odoo 17 ERP Module
Celine George
ย 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
MattVassar1
ย 
IoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdfIoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdf
roshanranjit222
ย 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
Derek Wenmoth
ย 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
ย 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
ย 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
nabaegha
ย 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
yarusun
ย 

Recently uploaded (20)

Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
ย 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
ย 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
ย 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
ย 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
ย 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
ย 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
ย 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
ย 
How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...How to stay relevant as a cyber professional: Skills, trends and career paths...
How to stay relevant as a cyber professional: Skills, trends and career paths...
ย 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
ย 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
ย 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
ย 
Post init hook in the odoo 17 ERP Module
Post init hook in the  odoo 17 ERP ModulePost init hook in the  odoo 17 ERP Module
Post init hook in the odoo 17 ERP Module
ย 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
ย 
IoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdfIoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdf
ย 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
ย 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
ย 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
ย 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
ย 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
ย 

Java stream

  • 2. 2 I/O Streams An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays. Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways. A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time. A program uses an output stream to write data to a destination, one item at time
  • 3. 3 The basic stream classes are defined in the package โ€œjava.ioโ€. ๏ƒ˜A Java program is reasonably well served by its default state when execution begins. Three streams are setup and ready to go. There are two output streams, identified by the objects System.out and System.err, and one input stream, identified by the object System.in. These objects are defined as public data fields in the System class of the java.lang package ๏ƒ˜The err and out objects are instances of the PrintStream class and the in object is an instance of the InputStream class. Java defines two types of streams. ๏ƒ˜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. Character stream uses Unicode and therefore can be internationalized.
  • 4. 4
  • 5. 5 Byte Stream Classes Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and OutputStream BufferedInputStream :Used for Buffered Input Stream. BufferedOutputStream: Used for Buffered Output Stream. DataInputStream: Contains method for reading java standard datatype DataOutputStream: An output stream that contain method for writing java standard data type
  • 6. 6 FileInputStreamInput: stream that reads from a file FileOutputStreamOutput: stream that write to a file. InputStream: Abstract class that describe stream input. OutputStream: Abstract class that describe stream output. PrintStreamOutput: Stream that contain print() and println() method Methods read() : reads byte of data. write() : Writes byte of data.
  • 7. 7 Character Stream Classes Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader and Writer. Charcter stream classes BufferedReaderHandles buffered input stream. BufferedWriterHandles buffered output stream. FileReaderInput stream that reads from file. FileWriterOutput stream that writes to file. InputStreamReaderInput stream that translate byte to character
  • 8. 8 OutputStreamReaderOutput stream that translate character to byte. PrintWriterOutput Stream that contain print() and println() method. ReaderAbstract class that define character stream input WriterAbstract class that define character stream output
  • 9. 9 We use the object of BufferedReader class to take inputs from the keyboard. 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. To read string we have to use readLine() function with BufferedReader class's object.
  • 10. 10 Scanner constructors Scanner(File source) Constructs a new Scanner that produces values scanned from the specified file. Scanner(InputStream source) Constructs a new Scanner that produces values scanned from the specified input stream. Scanner(Readable source) Constructs a new Scanner that produces values scanned from the specified source. Scanner(String source) Constructs a new Scanner that produces values scanned from the specified string.
  • 11. 11 Scanner will read a line of input from its source Scanner sc = new Scanner (System.in); int i = sc.nextInt(); System.out.println("You entered" + i); This example reads a single int from System.in and outputs it to System.out. It does not check that the user actually entered an int.
  • 12. 12 Next Methods String next() Finds and returns the next complete token from this scanner. boolean nextBoolean() Scans the next token of the input into a boolean value and returns that value. byte nextByte() Scans the next token of the input as a byte. double nextDouble() Scans the next token of the input as a double. float nextFloat() Scans the next token of the input as a float. int nextInt() Scans the next token of the input as an int. String nextLine() Advances this scanner past the current line and returns the input that was skipped. long nextLong() Scans the next token of the input as a long. short nextShort() Scans the next token of the input as a short.
  • 13. 13 hasNext methods boolean hasNext() Returns true if this scanner has another token in its input. boolean hasNextBoolean() Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false". boolean hasNextByte() Returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method. boolean hasNextDouble() Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. boolean hasNextFloat() Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method.
  • 14. 14 boolean hasNextInt() Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. boolean hasNextLine() Returns true if there is another line in the input of this scanner. boolean hasNextLong() Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. boolean hasNextShort() Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method.
  • 15. 15 RandomAccessFile The RandomAccessFile class in the Java IO API allows you to move around a file and read from it or write to it. We can replace existing parts of a file too. This is not possible with the FileInputStream or FileOutputStream. RandomAccessFile file = new RandomAccessFile("c:datafile.txt", "rw"); To read or write at a specific location in a RandomAccessFile you must first position the file pointer at the location to read or write. This is done using the seek() method. The current position of the file pointer can be obtained by calling the getFilePointer() method. RandomAccessFile file = new RandomAccessFile("c:datafile.txt", "rw"); file.seek(200); long pointer = file.getFilePointer(); file.close();
  • 16. 16 Reading from a RandomAccessFile Reading from a RandomAccessFile is done using one of it many read() methods. RandomAccessFile file = new RandomAccessFile("c:datafile.txt", "rw"); int aByte = file.read(); file.close(); The read() method reads the byte located a the position in the file currently pointed to by the file pointer in theRandomAccessFile instance.
  • 17. 17 Writing to a RandomAccessFile Writing to a RandomAccessFile can be done using one it its many write() methods. RandomAccessFile file = new RandomAccessFile("c:datafile.txt", "rw"); file.write("Hello World".getBytes()); file.close(); Just like with the read() method the write() method advances the file pointer after being called. That way you don't have to constantly move the file pointer to write data to a new location in the file.
  ็ฟป่ฏ‘๏ผš