[post-views]
In this lesson, you will learn.
There are two types of streams in Java as shown below.
Note: Input/Output Stream Classes are defined in the java.io package.
Byte Stream has two abstract classes.
Used to read data from a source. It has some key classes summarized below.
Key Classes of InputStream:
FileInputStream: Reads data from a file.BufferedInputStream: Adds buffering to an input stream.DataInputStream: Allows reading of Java primitive data types.ObjectInputStream: Used for reading objects.Used to write data to a destination. It has some key classes summarized below.
Key Classes of OutputStream:
FileOutputStream: Writes data to a File.BufferedOutputStream: Adds buffering to an output stream.DataOutputStream: Allows writing of Java primitive data types.ObjectOutputStream: Used for writing objects.
Character Streams are represented by two abstract classes.
Used to read data from a source. It has some key classes summarized below.
Key Classes:
FileReader: Reads character files.BufferedReader: Buffer’s characters for efficient reading.InputStreamReader: Bridges byte streams to character streams.StringReader: Reads characters from a string.Used to write data to a destination. It has some key classes summarized below.
Key Classes:
FileWriter: Writes characters to a file.BufferedWriter: Buffer’s characters for efficient writing.OutputStreamWriter: Bridges character streams to byte streams.StringWriter: Writes characters to a string.
InputStreamReader is a bridge from byte streams to character streams.Syntax
InputStreamReader isr = new InputStreamReader(System.in);
This is usually used to read bytes from an input stream (like System.in) and convert them into characters.
System.in is a standard input stream, typically connected to keyboard input.
BufferedReader reads text from a character input stream, buffering characters and providing efficient reading of characters, arrays, and lines.
Syntax
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
It buffers the input for efficient reading of characters, arrays, and lines and stores in an object br.
package ch3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SingleStringInput {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Enter your name:");
String name = reader.readLine();
System.out.println("Hello, " + name + "!");
}
}
Output
Enter your name:
Amit Kumar
Hello, Amit Kumar!
package ch3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IntegerArithmetic {
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter first number:");
int firstNumber = Integer.parseInt(reader.readLine());
System.out.println("Enter second number:");
int secondNumber = Integer.parseInt(reader.readLine());
int sum = firstNumber + secondNumber;
System.out.println("The sum is: " + sum);
}
}
Output
Enter first number:
10
Enter second number:
15
The sum is: 25
Explanation
Integer.parseInt(String s): This static method from the Integer class attempts to parse a String as an integer value.
It throws NumberFormatException if the string cannot be parsed as an integer.
IOException: This exception is thrown when an I/O operation fails or is interrupted. In this context, it could be thrown by the readLine() method if an I/O error occurs while reading input from the user.
package ch3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadSingleCharacter {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Enter a character: ");
int charAsInt = reader.read(); // Reads a single character
char character = (char) charAsInt;
System.out.println("You entered: " + character);
reader.close();
}
}
Output
First Run
Enter a character:
A
You entered: A
Second Run
Enter a character:
Hot Java
You entered: H
package ch3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MultiLineInput {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader
(new InputStreamReader(System.in));
String inputLine;
System.out.println("Enter lines of text (type 'stop' to quit):");
while (!(inputLine = reader.readLine()).equalsIgnoreCase("stop"))
{
System.out.println("You entered: " + inputLine);
}
}
}
Output
Enter lines of text (type 'stop' to quit):
Java is an object oriented programming language.
You entered: Java is an object oriented programming language.
Java is simple and platform independent language.
You entered: Java is simple and platform independent language.
stop
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.