XXXInputStream and XXXOutputStream (where XXX varies, there’s a lot of options) deal with 8-bit bytes. For example, OutputStream.write(byte[] c);
XXXWriter or XXXReader deal with 16-bit chars. For example, Reader.read(char[] cbuf)
.
OutputStreamWriter converts an OutputStream to a Writer. As you may have guessed, InputStreamReader converts from an InputStream to a Reader. I am unaware of any classes that do the reverse, i.e. convert a Reader to an InputStream.
FileWriter is a Writer that talks to files. Since a Java String internally uses chars (16 bit so they can handle Unicode), FileWriter is the natural class for use with Unicode Strings.
FileOutputStream is an OutputStream for writing bytes to a file. OutputStreams do not accept chars (or Strings). By wrapping it in an OutputStreamWriter you now have a Writer, which does accept Strings.
Now, the real question, is when do you use a Reader/Writer and when a Stream? I’ve used Java for years and sometimes I get confused too. I believe the following to be correct:
Handy Guide – How to Decide Which to use:
- If you are dealing with binary data (e.g. an image) use Streams.
- If you are using non-ASCII Unicode characters, e.g. Chinese, use
Readers/Writers. - If you are using ordinary ASCII text (the traditional 0-127 characters) you can (usually) use either.
Some other links:
inputstream and reader in Java IO
InputStream vs InputStreamReader