Since Java SE 7, there are more useful classes, and less direct touching of java.io [] classes. However, when I used InputStream [] live for the first time in a while the other day, I googled because I didn't know how to use it, so I made a diagram.
There are many classes I haven't used, so if you have a story about how to use it like this, please comment.
It became as follows. However, this is for clarity and is not accurate. The reason will be described later.
For various reasons, I dare to make it inaccurate.
The following classes are excluded.
These two have been deprecated due to a design mistake and have been excluded.
This class assumes a false perception that characters are properly represented by bytes.
Character to byte conversion is not done correctly in this class.
These two are excluded because they are never used alone. I wondered why I didn't design it to be abstract, but it seems that there are other people who have the same question.
java - Why FilterInputStream is not Abstract Class - Stack Overflow
I don't think I need this FilterOutputStream [] in the first place.
These are excluded because I couldn't find a suitable position. Perhaps the pipe should be a separate diagram.
I haven't used SequenceInputStream [], but it's a special class that concatenates InputStream [], so even if I put it in the figure, I wonder if it will be lonely in the corner.
For example, DataInputStream [], ObjectInputStream [] inherit from InputStream [], but omit it. DataOutputStream [], ObjectOutputStream [] inherits from OutputStream [], but omits it.
The reason is that these are used to read and write primitive types and objects, respectively, and there seems to be no other scene to pass them as InputStream.
If this is PushbackInputStream [], for example, it is supposed to be used as "read the first few bytes to automatically determine the file format, and then read the contents as InputStream []".
Also, ObjectInputStream [] directly inherits InputStream [], but I think the reason why DataInputStream [] is sandwiched between FilterInputStream [] is also difficult to understand. It seems that there are other people who have similar questions.
All of these have a naming convention of XXXInputStream
, XXXOutputStream
, XXXReader
, XXXWriter
. Therefore, it is difficult to distinguish and it is a source of confusion.
Encoding is subtle for the three classes with colored backgrounds in the figure.
FileReader [], FileWriter [] do not have a constructor to specify the encoding. So I put it in the figure, but you should ignore it. Some say that FileReader [] is slow, and now there is [Files # newBufferedReader] [].
PrintStream [] has PrintWriter [], so I'm not sure how to handle it.
The PrintWriter class is used in situations where it is necessary to write characters instead of bytes.
It says, but I wonder if it's just left for System.out [] and System.err [].
Looking at the figure, it was surprisingly simple (´ω`)
Also, I thought it would take some time to put the link (´ ・ ω ・ `)
Recommended Posts