Don’t sweat the load too much if you still feel somewhat dazed and confused after reading this chapter. The only way you get good at this stuff is through experience. Trust me when I say that you are not alone in your confusion. To help ease your pain and speed your understanding I have included table 18-3 which lists various combinations of the java.io classes based on intended usage.
If you want to… | Use the following class or class combination... |
---|---|
Byte-Oriented Stream Output | |
Write long streams of bytes | FileOutputStream |
Write long streams of bytes with improved output efficiency | FileOutputStream BufferedOutputStream |
Write streams of bytes and primitive type values | FileOutputStream DataOutputStream |
Write streams of bytes and primitive types with improved efficiency | FileOutputStream BufferedOutputStream DataOutputStream |
Write serialized objects | FileOutputStream ObjectOutputStream |
Write serialized objects with improved efficiency | FileOutputStream BufferedOutputStream ObjectOutputStream |
Write textual representations of primitive types and objects | FileOutputStream PrintStream |
Write textual representation of primitive types and objects with improved efficiency | FileOutputStream BufferedOutputStream PrintStream |
Byte-Oriented Stream Input | |
Read long streams of bytes | FileInputStream |
Read long streams of bytes with improved efficiency | FileInputStream BufferedInputStream |
Read serialized objects from disk | FileInputStream ObjectInputStream |
Read serialized objects from disk with improved efficiency | FileInputStream BufferedInputStream ObjectInputStream |
Character-Oriented Stream Output | |
Write text to disk in default local encoding | FileWriter |
Write text to disk in default local encoding with improved efficiency | FileWriter BufferedWriter |
Write characters to disk with more control over character encoding scheme | FileOutputStream OutputStreamWriter |
Write characters to disk with more control over character encoding and with improved efficiency | FileOutputStream OutputStreamWriter BufferedWriter |
Write textual representations of primitive types and objects with control over character encoding and with improved efficiency | FileOutputStream OutputStreamWriter BufferedOutputStream PrintWriter |
Character-Oriented Stream Input | |
Read text from file in default local encoding | FileReader |
Read text from file in default local encoding with improved efficiency | FileReader BufferedReader |
Read text from file with more control over character encoding | FileInputStream InputStreamReader |
Read text from file with more control over character encoding and with improved efficiency | FileInputStream InputStreamReader BufferedReade |
Random Access File Input and Output | |
Read and write bytes, characters, and primitive type values anywhere within a file | RandomAccessFile |