What is the difference between InputStream and Reader in Java? When would you use each?

InputStream and Reader in Java

InputStream:

  • Used for reading binary data (e.g., images, videos, raw data).
  • Operates on bytes (8-bit data).
  • Does not handle character encoding.
  • Examples: FileInputStream, BufferedInputStream.
  • Use when working with binary files or streams like images or audio.

Reader:

  • Used for reading text data (e.g., text files, strings).
  • Operates on characters (16-bit Unicode).
  • Automatically handles character encoding (e.g., UTF-8).
  • Examples: FileReader, BufferedReader.
  • Use when dealing with text or character data.

When to Use:

  • InputStream for binary data (images, audio).
  • Reader for text or character data (text files).