0% found this document useful (0 votes)
6 views

Module 10 Input and Output Stream

Uploaded by

Yeoj Bongon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 10 Input and Output Stream

Uploaded by

Yeoj Bongon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Object Oriented Programming

(Java)
Module 10
Input and Output Stream
Learn how to read from input streams and write to
output streams
Learn how to write a text and binary files
InputStream and OutputStream

10.1

InputStream and OutputStream


Java I/O

ØJava I/O (Input and Output) is used to


process the input and produce the output.
ØJava uses the concept of a stream to make
I/O operation fast. The java.io package
contains all the classes required for input and
output operations.
ØWe can perform file handling in Java by
Java I/O API.

Input and Output Stream


Stream

Ø A stream is a sequence of data. In Java, a stream is composed of


bytes. It's called a stream because it is like a stream of water that
continues to flow.
Ø In Java, 3 streams are created for us automatically. All these
streams are attached with the console.
Ø 1) System.out: standard output stream
Ø 2) System.in: standard input stream
Ø 3) System.err: standard error stream

Input and Output Stream


Stream

Ø Let's see the code to print output and an error message to the
console.

Ø Let's see the code to get input from console.

Input and Output Stream


OutputStream vs InputStream

Ø OutputStream
Ø Java application uses an output stream to write data to a
destination; it may be a file, an array, peripheral device or
socket.

Ø InputStream
Ø Java application uses an input stream to read data from a
source; it may be a file, an array, peripheral device or socket.

Input and Output Stream


OutputStream vs InputStream

Input and Output Stream


OutputStream Class

Ø OutputStream Class
Ø Java OutputStream class is an abstract class. It is the
superclass of all classes representing an output stream of
bytes. An output stream accepts output bytes and sends them
to some sink.

Input and Output Stream


OutputStream Class

Ø Useful methods of OutputStream Class

Input and Output Stream


OutputStream Class

Ø OutputStream Hierarchy

Input and Output Stream


InputStream Class

Ø InputStream class is an abstract class. It is the superclass of all


classes representing an input stream of bytes.

Ø Useful methods of InputStream

Input and Output Stream


InputStream Class

Ø InputStream Hierarchy

Input and Output Stream


Java FileOutputStream Class

Ø Java FileOutputStream is an output stream used for writing data


to a file.

Ø If you have to write primitive values into a file, use


FileOutputStream class. You can write byte-oriented as well as
character-oriented data through FileOutputStream class. But, for
character-oriented data, it is preferred to use FileWriter than
FileOutputStream.

Ø FileOutputStream class declaration for Java.io.FileOutputStream


class:

Input and Output Stream


FileOutputStream class methods

Input and Output Stream


Java FileOutputStream Example 1: write byte
Output:

The content of a text


file testout.txt is set with the
data A.

testout.txt

Input and Output Stream


Java FileOutputStream Example 2: write string
Output:

The content of a text


file testout.txt is set with the
data Welcome to javaTpoint.

testout.txt

Input and Output Stream


Java FileInputStream Class

Ø Java FileInputStream class obtains input bytes from a file. It is


used for reading byte-oriented data (streams of raw bytes) such as
image data, audio, video etc. You can also read character-stream
data. But, for reading streams of characters, it is recommended to
use FileReader class.

Ø FileInputStream class declaration for Java.io.FileInputStream


class:

Input and Output Stream


Java FileInputStream class methods

Input and Output Stream


Java FileInputStream example 1: read single character

Note: Before running the code, a


text file named as "testout.txt" is
required to be created. In this file,
we are having following content:

After executing the above program, you will get a


single character from the file which is 87 (in byte
form). To see the text, you need to convert it into
character.
Output:

Input and Output Stream


Java FileInputStream example 2: read all characters

Output:

Input and Output Stream


10.2

Input and
FileWriter andOutput Stream
FileReader
Java FileWriter Class

Ø Java FileWriter class is used to write character-oriented data to


a file. It is character-oriented class which is used for file handling
in java.
Ø Unlike FileOutputStream class, you don't need to convert string
into byte array because it provides method to write string directly.
Ø Java.io.FileWriter

Input and Output Stream


Java FileWriter Class

Ø Constructors of FileWriter class

Input and Output Stream


Java FileWriter Class

Ø Methods of FileWriter class

Input and Output Stream


Java FileWriter Example

In this example, we are writing the data in the file


testout.txt using Java FileWriter class.

Input and Output Stream


Java FileReader Class

Ø Java FileReader class is used to read data from the file. It returns
data in byte format like FileInputStream class.
Ø It is character-oriented class which is used for file handling
in java.

Ø Java.io.FileReader

Input and Output Stream


Java FileReader Class

Ø Constructors of FileReader class

Input and Output Stream


Java FileReader Class

Ø Methods of FileReader class

Input and Output Stream


Java FileReader Example

In this example, we are reading the data from the text


file testout.txt using Java FileReader class.

Input and Output Stream


Backiel, A. (2015). Beginning Java Programming: The Object-oriented Approach. 1st Edition
Fain, Y. (2015). Java programming 24-hour trainer. John Wiley and Sons:Indianapolis, IN
Smith, J. (2015). Java programs to accompany Programming logic and design. Cengage Learning:Boston, MA
Yener, M (2015). Professional Java EE Design Patterns. John Wiley & Sons, Inc.
Gordon, S. (2017). Computer Graphics Programming in opengl with Java. Mercury Learning and Information
Butler, A (2017). Java. s.n
Lowe, D (2017). Java all-in-one for dummies. John Wiley and Sons
Saumont, P (2017). Functional Programming in Java: How Functional Techniques Improve Your Java Programs. 1st Edition
Heffelfinger, D. (2017). Java EE 8 Application Development. Packt Puublishing
Murach, J. (2017). Murach’s Java Programming. Mike Murach & Associates,Inc.
Burd, B. (2017). Beginning programming with Java for dummies. John Wiley & Sons, Inc.
Manelli, L. (2017). Developing java web application in a day. s.n.
Klausen, P. (2017). Java 5: Files and Java IO software development (e-book)
Klausen, P. (2017). Java 3 object-oriented programming software development (e-book)
Muller, M (2018). Practical JSF in Java EE 8. Apress

You might also like