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

I/O Streams-Basics Byte Streams and Character Streams, Reading Console Input

This document discusses I/O streams in Java. It covers the basics of byte streams and character streams, and describes Java's predefined streams System.in, System.out, and System.err. It also discusses how to read input from the console using a BufferedReader, including how to read individual characters and strings. Key classes covered include InputStream, OutputStream, Reader, Writer, BufferedReader, and InputStreamReader.

Uploaded by

Ganesh Nelluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

I/O Streams-Basics Byte Streams and Character Streams, Reading Console Input

This document discusses I/O streams in Java. It covers the basics of byte streams and character streams, and describes Java's predefined streams System.in, System.out, and System.err. It also discusses how to read input from the console using a BufferedReader, including how to read individual characters and strings. Key classes covered include InputStream, OutputStream, Reader, Writer, BufferedReader, and InputStreamReader.

Uploaded by

Ganesh Nelluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

I/O Streams- Basics

Byte Streams and


Character Streams,
Reading console input
I/O Basics
• In fact, aside from print( ) and println( ), none
of the I/O methods have been used
significantly.
• The reason is simple: most real applications of
Java are not text-based, console programs.
I/O Basics-Cont...
• Text-based console I/O is just not very
important to Java programming.
• Java does provide strong, flexible support
for I/O as it relates to files and networks.
• Java’s I/O system is cohesive and consistent.
I/O Basics-Cont...
• Graphically oriented programs rely upon Java’s
Abstract Window Toolkit (AWT) or Swing for
interaction with the user.
• Although text-based programs are excellent as
teaching examples, they do not constitute an
important use for Java in the real world.
• Also, Java’s support for console I/O is limited and
somewhat awkward to use—even in simple
example programs.
Streams
• 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 physical devices to which they are
linked differ.
Streams-Cont...
• Thus, the same I/O classes and methods can be
applied to any type of device.
• This means that an input stream can abstract
many different kinds of input: from a
– disk file
– keyboard
– network socket
• Likewise, an output stream may refer to the
console, a disk file, or a network connection.
Byte Streams and Character Streams

• Java defines two types of streams:


byte and character.
• Byte streams provide a convenient means for
handling input and output of bytes.
• Byte streams are used, for example, when
reading or writing binary data.
• Character streams provide a convenient means
for handling input and output of characters.
• Character streams use Unicode and, therefore, can
be internationalized.
• Unicode defines a fully international character set
that can represent all of the characters found in all
human languages. It is a unification of dozens of
character sets, such as Latin, Greek, Arabic, Cyrillic,
Hebrew, Katakana, Hangul, and many more.
• At the time of Java's creation, Unicode required 16
bits. Thus, in Java char is a 16-bit type. The range of a
char is 0 to 65,536.
• There are no negative chars.
• Also, in some cases, character streams are
more efficient than byte streams.
• The original version of Java (Java 1.0) did not
include character streams and, thus, all I/O
was byte-oriented.
• Character streams were added by Java 1.1,
and certain byte-oriented classes and
methods were deprecated.
• This is why older code that doesn’t use
character streams should be updated to take
advantage of them, where appropriate.
• One other point: at the lowest level, all I/O is
still byte-oriented.
The Byte Stream Classes

• Byte streams are defined by using two class


hierarchies.
• At the top are two abstract classes:
InputStream and OutputStream.
• Each of these abstract classes has several
concrete subclasses that handle the differences
between various devices, such as disk files,
network connections, and even memory
buffers.
The Byte Stream Classes-Cont...

• The byte stream classes are shown in Table


13-1.
• Remember, to use the stream classes, you
must import java.io
• The abstract classes InputStream and
OutputStream define several key methods
that the other stream classes implement.
The Byte Stream Classes-Cont...

• Two of the most important are read( ) and


write( ), which, respectively, read and write
bytes of data.
• Both methods are declared as abstract inside
InputStream and OutputStream.
• They are overridden by derived stream
classes.
TABLE 13-1 The Byte Stream Classes
TABLE 13-1 The Byte Stream Classes-Cont...
The Character Stream Classes

• Character streams are defined by using two class


hierarchies.
• At the top are two abstract classes, Reader and
Writer.
• These abstract classes handle Unicode character
streams.
• Java has several concrete subclasses of each of these.
• The character stream classes are shown in Table 13-2.
The Character Stream Classes-Cont...

• The abstract classes Reader and Writer define


several key methods that the other stream
classes implement.
• Two of the most important methods are
read( ) and write( ), which read and write
characters of data, respectively.
• These methods are overridden by derived
stream classes.
Table 13-2. The Character Stream I/O Classes
Table 13-2. The Character Stream I/O Classes-
Cont...
The Predefined Streams

• As you know, all Java programs automatically


import the java.lang package.
• This package defines a class called System,
which encapsulates several aspects of the run-
time environment.
• For example, using some of its methods, you
can obtain the current time and the settings of
various properties associated with the system.
The Predefined Streams-Cont...

• System also contains three predefined stream


variables: in, out, and err.
• These fields are declared as public, static, and
final within System.
• This means that they can be used by any other
part of your program and without reference to
a specific System object.
The Predefined Streams-Cont...

• System.out refers to the standard output


stream. By default, this is the console.
• System.in refers to standard input, which is
the keyboard by default.
• System.err refers to the standard error
stream, which also is the console by default.
• However, these streams may be redirected to
any compatible I/O device.
The Predefined Streams-Cont...

• System.in is an object of type InputStream;


System.out and System.err are objects of type
PrintStream.
• These are byte streams, even though they
typically are used to read and write
characters from and to the console.
• As you will see, you can wrap these within
characterbased streams, if desired.
Reading Console Input
• In Java, console input is accomplished by
reading from System.in.
• To obtain a characterbased stream that is
attached to the console,
wrap System.in in a BufferedReader object.
• BufferedReader supports a buffered input
stream.
• Its most commonly used constructor is shown
here:
BufferedReader(Reader inputReader)
• Here, inputReader is the stream that is linked
to the instance of BufferedReader that is being
created.
• Reader is an abstract class.
• One of its concrete subclasses is
InputStreamReader,
which converts bytes to characters.
• To obtain an InputStreamReader object that is
linked to System.in, use the following
constructor:
InputStreamReader(InputStream inputStream)
• Because System.in refers to an object of type
InputStream, it can be used for inputStream.
• Putting it all together, the following line of code
creates a BufferedReader that is connected to the
keyboard:
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

After this statement executes, br is a character-based


stream that is linked to the console through
System.in.
Reading Characters
• To read a character from a BufferedReader, use
read( ).
• The version of read( ) that we will be using is
int read( ) throws IOException Each time that
read( ) is called, it reads a character from the
input stream and returns it as an integer value. It
returns –1 when the end of the stream is
encountered.
• As you can see, it can throw an IOException
Reading Characters-Cont…
• The following program demonstrates read( )
by reading characters from the console until
the user types a "q.” Notice that any I/O
exceptions that might be generated are simply
thrown out of main( ).
• Such an approach is common when reading
from the console, but you can handle these
types of errors yourself, if you chose.
LTC: Use a BufferedReader to read characters
from the console
// Use a BufferedReader to read characters
//from the console.
import java.io.*;
class BRRead {
public static void main(String args[])
throws IOException
{
char c;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to
quit.");
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
Reading Characters-Cont…
Here is a sample run:
Enter characters, 'q' to quit.
123abcq
1
2
3
a
b
c
q
Reading Characters-Cont…
• This output may look a little different from
what you expected,
because System.in is linebuffered, by default.
• This means that no input is actually passed to
the program until you press ENTER.
• As you can guess, this does not make read( )
particularly valuable for interactive console
input.
Reading Strings
• To read a string from the keyboard, use the
version of readLine( ) that is a member of the
BufferedReader class.
• Its general form is shown here:
String readLine( ) throws IOException
• As you can see, it returns a String object.
LTC: Program to read a string from console
using a BufferedReader
The following program demonstrates BufferedReader
and the readLine( ) method;
The program reads and displays lines of text until
you enter the word “stop”:
import java.io.*;
class BRReadLines {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}

End of session

You might also like