The InputStreamReader class reads characters from a byte input stream. It reads bytes and decodes them into characters using a specified charset. The decoding layer transforms bytes to chars according to an encoding standard . There are many available encodings to choose from.
InputStreamReader class performs two tasks:
- Read input stream of keyboard.
- Convert byte streams to character streams.
The following Java program obtain an InputStreamReader from keyboard
The following Java program obtain an InputStreamReader from a file
How do I convert a String to an InputStream in Java?
ByteArrayInputStream does the trick from Java 1.4
From Java 1.7, StandardCharsets defines constants for Charset including UTF-8 . You should include import java.nio.charset.StandardCharsets; in your Java file. Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8 .
The following Java program read a String as InputStream.