MODULE 3
CONTENT
• Exceptions, I/O and Threads Input and Output in Java: The File Class,
Standard Streams, Keyboard Input, File I/O Using Byte Streams,
Character Streams, File I/O Using Character Streams - Buffered
Streams, File I/O Using a Buffered Stream, Keyboard Input Using a
Buffered Stream, Writing Text Files. Threads: Threads vs. Processes,
Creating Threads by Extending Thread, Creating Threads by
Implementing Runnable, Advantages of Using Threads, Daemon
Threads, Thread States, Thread Problems, Synchronization.
Exceptions: Exception Handling, The Exception Hierarchy, throws
statement, throw statement, Developing user defined Exception
Classes- The finally Block.
Streams
• Java programs perform I/O through streams.
• A stream is an abstraction that either produces or consumes information.
• A stream is a sequence of objects that supports various methods.
• A stream is linked to a physical device by the Java I/O system.
– Input stream may refer to different kinds of input:
• from a disk file, a keyboard, or a network socket
– Output stream may refer to
• the console, a disk file, or a network connection.
Working of Javava I/O stream
• Stream is like a flow of data.
Streams
• The [Link] package contains all the classes required for input and
output operations.
• Java defines two types of streams: byte and character.
• Byte streams provides a means for handling input and output of
bytes.
– Byte streams are used when reading or writing binary data.
• Character streams provide a means for handling input and output of
characters.
– in some cases, character streams are more efficient than byte
streams.
ByteStream 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.
• Two of the most important are read( ) and write( ),
– These methods are overridden by derived stream classes.
ByteStream classes
• ByteStream classes
ByteStream classes
Byte Stream I/O classes
Character streams
• Character streams are defined by using two class hierarchies. At the top are
two abstract classes,
– Reader and Writer.
• Java has several concrete subclasses of each of these.
• Two of the most important methods are read( ) and
write( ).
– These methods are overridden by derived stream classes.
CharacterStream classes
The Predefined Streams
• All Java programs automatically import the [Link] package. This
package defines a class called System.
• System contains three predefined stream variables:
– in, out, and err.
– These fields are declared as public, static, and final within System.
• [Link] refers to the standard output stream.
• [Link] refers to standard input, which is the keyboard by default.
• [Link] refers to the standard error stream, which is the console by default.
Reading Console Input
• The preferred method of reading console input is to use a
character-oriented stream.
• In Java, console input is accomplished by reading from [Link].
• – To obtain a character based stream that is attached to the
console, wrap [Link] in a BufferedReader object.
Reading Console Input(contd.)
• BufferedReader supports a buffered input stream.
– Its most commonly used constructor is:
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.
Reading Console Input(contd.)
• One of the concrete subclasses of
InputStreamReader.
• InputStreamReader converts bytes to characters.
• –It reads bytes and decodes them into characters using a specified charset.
• To obtain an InputStreamReader object that is linked to
[Link], the constructor that can be used is :
• InputStreamReader(InputStream inputStream)
• Following line of code creates a BufferedReader that is connected
to the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
• By wrapping the [Link] (standard input stream) in an
InputStreamReader which is wrapped in a BufferedReader, we can read
input from the user in the command line.
• After this statement executes, br is a character-based stream that is linked to
the console through [Link]
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
• [Link] keyboard(Byte stream)
• Convert byte steam to character stream using
InputStreamReader.
• Then wrap that character stream in a buffered stream
BufferedReader
Reading Characters(E.g.)
import [Link].*;
class Readinp
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a letter");
c=(char)[Link]();
[Link](“Letter="+c);
OUTPUT
Enter a letter
} a
} Letter=a
Read console inputs
Q. Write a Java program to display Fibonacci
series up to a limit using BufferedReader?
import [Link].*;
class Fib
{
public static void main(String args[]) throws IOException
{
int i,a=0,b=0,f=0;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter [Link] elements in Fibonacci series: ");
n = [Link]([Link]())
[Link]("Fibonacci Series:");
while(f<=n)
{
[Link](f);
a=b;
b=f;
f=a+b;
}
}
}