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

PPTX
Understanding java streams
ODP
IO In Java
PDF
Java IO
PDF
Java Course 8: I/O, Files and Streams
PPTX
Java Input Output (java.io.*)
PPTX
L21 io streams
PDF
Java I/o streams
PPT
Java Streams
Understanding java streams
IO In Java
Java IO
Java Course 8: I/O, Files and Streams
Java Input Output (java.io.*)
L21 io streams
Java I/o streams
Java Streams

What's hot (20)

PPTX
Handling I/O in Java
PPT
Input output streams
PDF
Java - File Input Output Concepts
PPS
Files & IO in Java
PDF
PDF
32.java input-output
PPT
Java IO Package and Streams
PDF
PDF
java.io - streams and files
PPT
Byte stream classes.49
PPT
Chapter 12 - File Input and Output
PPT
Various io stream classes .47
PPTX
Java file
PDF
Javaiostream
PDF
input/ output in java
PPT
Character stream classes introd .51
PPT
IO and serialization
PPTX
PPTX
Java I/O
PPT
File Input & Output
Handling I/O in Java
Input output streams
Java - File Input Output Concepts
Files & IO in Java
32.java input-output
Java IO Package and Streams
java.io - streams and files
Byte stream classes.49
Chapter 12 - File Input and Output
Various io stream classes .47
Java file
Javaiostream
input/ output in java
Character stream classes introd .51
IO and serialization
Java I/O
File Input & Output
Ad

Similar to Java stream (20)

DOCX
Oodp mod4
PPTX
File Input and output.pptx
PDF
Java IO Stream, the introduction to Streams
PPT
Stream Based Input Output
DOCX
Unit IV Notes.docx
PPTX
Stream In Java.pptx
PDF
UNIT4-IO,Generics,String Handling.pdf Notes
PPT
ppt on scanner class
PPTX
I/O Streams
PPTX
Java Input and Output
PPT
Itp 120 Chapt 19 2009 Binary Input & Output
PPTX
Computer science input and output BASICS.pptx
PPT
Io Streams
PPT
Java development development Files lectur6.ppt
PDF
File Handling in Java.pdf
PPTX
Java Tutorial Lab 6
DOC
Web Technology Web Technology Notes Streams Network Principles and SocketsUni...
PPTX
Buffer and scanner
PDF
Class notes(week 5) on command line arguments
PDF
Nhap xuat trong java
Oodp mod4
File Input and output.pptx
Java IO Stream, the introduction to Streams
Stream Based Input Output
Unit IV Notes.docx
Stream In Java.pptx
UNIT4-IO,Generics,String Handling.pdf Notes
ppt on scanner class
I/O Streams
Java Input and Output
Itp 120 Chapt 19 2009 Binary Input & Output
Computer science input and output BASICS.pptx
Io Streams
Java development development Files lectur6.ppt
File Handling in Java.pdf
Java Tutorial Lab 6
Web Technology Web Technology Notes Streams Network Principles and SocketsUni...
Buffer and scanner
Class notes(week 5) on command line arguments
Nhap xuat trong java
Ad

More from Arati Gadgil (16)

PPT
Java adapter
PPT
Java swing
PPT
Java applet
PPT
Java layoutmanager
PPT
Java awt
PPT
Java thread
PPT
Java networking
PPT
Java jdbc
PPT
Java package
PPT
Java interface
PPT
Java inheritance
PPT
Java eventhandling
PPT
Java exception
PPT
Java collection
PPT
Java class
PPT
Java basic
Java adapter
Java swing
Java applet
Java layoutmanager
Java awt
Java thread
Java networking
Java jdbc
Java package
Java interface
Java inheritance
Java eventhandling
Java exception
Java collection
Java class
Java basic

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pharma ospi slides which help in ospi learning
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Open folder Downloads.pdf yes yes ges yes
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
Module 3: Health Systems Tutorial Slides S2 2025
Open Quiz Monsoon Mind Game Prelims.pptx
O7-L3 Supply Chain Operations - ICLT Program
Cardiovascular Pharmacology for pharmacy students.pptx
Pre independence Education in Inndia.pdf
Revamp in MTO Odoo 18 Inventory - Odoo Slides
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Renaissance Architecture: A Journey from Faith to Humanism

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.