Input/Output
G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Overview of topics
I/O Basics
Reading Console Input
Writing Console Output
Print Writer Class
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 2
Java programs perform I/O through streams
A stream is an abstraction that either produces or consumes
information.
A stream is linked to a physical device by the Java I/O system.
All streams behave in the same manner, even if the actual
Input/Output physical devices to which they are linked differ.
I/O Basics The same I/O classes and methods can be applied to different
Reading Console Input types of devices.
Writing Console Output An input stream can abstract different kinds of input: disk file,
Print Writer Class a keyboard, or a network socket.
An output stream may refer to the console, a disk file, or a
network connection
Java implements streams within class hierarchies defined in
the [Link] package.
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 3
I/O Basics
Java defines two types of streams
Byte streams
Character streams
Byte streams
Provide a convenient means for handling input and output of bytes.
Are used, for example, when reading or writing binary data.
Character streams
Provide a convenient means for handling input and output of characters.
They use Unicode and, therefore, can be internationalized.
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 4
Byte Stream Classes
Byte streams are defined by using two class
hierarchies.
Top two abstract classes:
InputStream
OutputStream
These abstract classes has several concrete
subclasses that handle the differences among various
devices, such as disk files, network connections, and
memory buffers.
InputStream and OutputStream define several key
methods that other stream classes implement.
Important ones: read( ), write( )
Each has a form that is abstract and must be
overridden by derived stream classes
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 5
Byte Stream Classes
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 6
Character Stream Classes
Character streams are defined by using two
class hierarchies.
Top two abstract classes:
Reader
Writer
These abstract classes handle Unicode
character streams and has several concrete
subclasses for each of these
Abstract classes Reader and Writer define
several key methods that other stream classes
implement.
Important methods: read( ), write( ), which
read and write characters of data
Each has a abstract form that must be
overridden by derived stream classes
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 7
Character Stream Classes
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 8
Predefined Streams
All Java programs automatically import the [Link] package.
[Link] defines a class called System, which encapsulates several aspects of the run-time
environment
System contains three predefined stream variables
in, out, and err.
These fields are declared as public, static, and final within System.
[Link] refers to standard input, which is the keyboard by default.
[Link] refers to the standard output stream, console by default.
[Link] refers to the standard error stream, console by default.
These streams may be redirected to any compatible I/O device.
[Link] is an object of type InputStream;
[Link] and [Link] are objects of type PrintStream.
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 9
Reading Console Input
Console input is read from [Link].
Character based stream attached to the console, wrap [Link] in a BufferedReader object.
BufferedReader supports a buffered input stream.
Commonly used constructor: BufferedReader(Reader inputReader)
inputReader is the stream that is linked to the instance of BufferedReader that is being created.
One of the concrete subclass of abstract class Reader is InputStreamReader, which converts bytes to
characters.
InputStreamReader object linked to [Link] is obtained using the following constructor:
InputStreamReader(InputStream inputStream)
Combining all, the following line of code creates a BufferedReader that is connected to the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
br is a character-based stream that is linked to the console through [Link].
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 10
Reading Characters
To read a character from a BufferedReader, use read( ).
int read( ) throws IOException
Each time read( ) is called,
it reads a character from the input stream
returns it as an integer value
returns –1 when the end of stream is encountered
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 11
Reading Characters
import [Link].*;
class BRRead {
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter characters, 'q' to quit.");
// read characters
do {
c = (char) [Link]();
[Link](c);
} while(c != 'q');
}
}
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 12
Reading Strings
readLine( ), a member of the BufferedReader class is used to read a string from the keyboard.
General form:
String readLine( ) throws IOException
Returns a String object.
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 13
Reading Strings
// Read a string from console using a BufferedReader.
import [Link].*;
class BRReadLines {
public static void main(String args[]) throws IOException
{
// create a BufferedReader using [Link]
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String str;
[Link]("Enter lines of text.");
[Link]("Enter 'stop' to quit.");
do {
str = [Link]();
[Link](str);
} while();
}
}
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 14
Writing Console Output
Console output is accomplished with print( ) and println( ) defined by the PrintStream
PrintStream is type of object referenced by [Link]
Since PrintStream is an output stream derived from OutputStream, it also implements the
low-level method write( ) to write to the console.
void write(int byteval)
writes the byte specified by byteval
// Demonstrate [Link]().
class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
[Link](b);
[Link]('\n');
}
}
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 15
Reading & Writing using Console object
Java SE 6 adds the Console class.
Console supplies no constructors.
A Console object is obtained by calling [Link]( )
static Console console( )
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 16
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 17
Reading & Writing using Console object
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 18
PrintWriter Class
PrintWriter is a character-based classes.
One of the constructor
PrintWriter(OutputStream outputStream, boolean flushingOn)
outputStream is an object of type OutputStream
flushingOn controls whether Java flushes the output stream every time a println( ) method is called. If
flushingOn is true, flushing automatically takes place. If false, flushing is not automatic.
PrintWriter supports the print( ) and println( ) methods.
If an argument is not a simple type, the PrintWriter methods call the object’s toString( ) method and then
display the result.
To write to the console by using a PrintWriter, specify [Link] for the output stream and automatic flushing.
Ex: Creates a PrintWriter that is connected to console output:
PrintWriter pw = new PrintWriter([Link], true);
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 19
PrintWriter Class
// Demonstrate PrintWriter
import [Link].*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter([Link], true);
[Link]("This is a string");
int i = -7;
[Link](i);
double d = 4.5e-7;
[Link](d);
}
}
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 20
End of Input Output
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 21