An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset
.
Constructor
InputStreamReader(InputStream in)
: Creates an InputStreamReader that uses the default charset. It wraps the given input stream in
as it acts as a bridge.
InputStreamReader(InputStream in, String charsetName)
: Creates an InputStreamReader that uses the named charset.
InputStreamReader(InputStream in, Charset cs)
: Creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream in, CharsetDecoder dec)
: Creates an InputStreamReader that uses the given charset decoder.
import java.io.*;
public class InputStreamReaderExample {
public static void main(String[] args) {
readFileExample();
readByteArrayExample();
readFileWithDifferentCharset();
}
// 1. Reading from a File
public static void readFileExample() {
try (FileInputStream fileInputStream = new FileInputStream("example.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")) {
System.out.println("Reading from file example.txt:");
int data = inputStreamReader.read();
while (data != -1) {
System.out.print((char) data);
data = inputStreamReader.read();
}
System.out.println("n");
} catch (IOException e) {
e.printStackTrace();
}
}
// 2. Reading from a Byte Array
public static void readByteArrayExample() {
String input = "This is a sample string to be read as byte array.";
byte[] byteArray = input.getBytes();
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream, "UTF-8")) {
System.out.println("Reading from byte array:");
int data = inputStreamReader.read();
while (data != -1) {
System.out.print((char) data);
data = inputStreamReader.read();
}
System.out.println("n");
} catch (IOException e) {
e.printStackTrace();
}
}
// 3. Specifying Different Charsets
public static void readFileWithDifferentCharset() {
try (FileInputStream fileInputStream = new FileInputStream("example.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "ISO-8859-1")) {
System.out.println("Reading from file example.txt with ISO-8859-1 charset:");
char[] buffer = new char[100];
int charsRead = inputStreamReader.read(buffer, 0, buffer.length);
while (charsRead != -1) {
System.out.print(new String(buffer, 0, charsRead));
charsRead = inputStreamReader.read(buffer, 0, buffer.length);
}
System.out.println("n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Common Methods
read()
: Reads a single character.
read(char[] cbuf, int offset, int length)
: Reads characters into a portion of an array. cbuf: buffer array into which the characters will be read. offset: starting position in the buffer array where the read characters will be stored. length: maximum number of characters to read from the stream.
getEncoding
: Returns the name of the character encoding being used by this stream.
ready()
: return true if this stream is ready to be read.
Reading character by character from keyword
import java.io.*;
public class InputStreamReaderExample {
public static void main(String[] args) {
// Create an InputStreamReader using System.in (standard input - keyboard)
InputStreamReader isr = new InputStreamReader(System.in);
// Buffer to store the read character
char[] buffer = new char[1];
try {
System.out.println("Enter characters, 'q' to quit:");
while (true) {
// Read a single character into the buffer
int numCharsRead = isr.read(buffer, 0, 1);
// Check if the end of the stream has been reached
if (numCharsRead == -1) {
break;
}
// Get the character from the buffer
char ch = buffer[0];
// Print the read character
System.out.print("You entered: ");
System.out.println(ch);
// Exit the loop if 'q' is entered
if (ch == 'q') {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// Close the InputStreamReader
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LineReadingExample {
public static void main(String[] args) {
// Creating an InputStreamReader that reads from standard input (keyboard)
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
// Wrapping the InputStreamReader with a BufferedReader for efficient reading
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
System.out.println("Enter text (type 'exit' to quit):");
String inputLine;
try {
// Continuously read lines of text from the keyboard
while ((inputLine = bufferedReader.readLine()) != null) {
// Print the entered line
System.out.println("You entered: " + inputLine);
// Break the loop if the user types 'exit'
if ("exit".equalsIgnoreCase(inputLine)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the BufferedReader and InputStreamReader
try {
bufferedReader.close();
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}