Overall structure
IO should be the most commonly used in the project, whether it is a simple file read, or server port monitoring, will use the IO, but in the face of Java IO package complex IO base Class library, often very big, is this choice InputStream or should choose Reader , should InputStream be Do you want to add one? Buffer How to choose the right class library according to the application scenario is a difficult problem in front of many code personnel, here I have the Java IO package of common libraries do a comb, including their organizational structure, functional characteristics, suitable scenarios, so as to facilitate the subsequent use of convenient and quick to select the most appropriate IO class according to the needs
Based on the analytic diagram, the two dimensions of Io can be divided from a large level:
- Data type, that is, a character or byte, on a class that corresponds to
Writer/Reader orOutputStream/InputStream
- The type of operation, that is: Read (input) or write (output), on the class corresponding to
Reader/InputStream orWriter/OutputStream
Tips for use
All the operations of Java IO are no different from the four main classes of the two dimensions of the extension, most simple, the following for a slightly difficult to understand and noteworthy points for a separate description
- Java IO is heavily used in decorator mode, so the IO class library is generally used when using the decorator call method:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
- In Java IO, whether the character read and write or byte read and write, the basic is one by one corresponding, in other words, there is a specific read class, generally there will be a specific write class, grasp this point, can play the role of simplification, such as
FileInputStream and FileOutputStream on the corresponding to the document read and write; PipedInputStreamand PipedOutputStream corresponding to the reading and writing of the pipe data respectively
- How to distinguish between read or write, this is a problem that often bothers the novice, here I provide a simple way to distinguish: with your current running program as a benchmark (that is, the program called IO), data from the program to other places to write (output), the data from other places to the program is read (input) If the data generated by the program is stored in a log file, it is written, the data is obtained from the port and processed in the program, it is read
- Depending on the action to be performed and the target data type, combined with the use of the scene to select the appropriate IO class to assemble, note that the above sentence actually involves three steps:
- Perform the action, that is, to read or to write
- Data type, that is, bytes or characters, sometimes it is necessary to convert characters and bytes (such as
OutputStreamWriter ), such as the need to store data on the network or memory, usually in the form of bytes, or file writing, the text class of the file itself is a character encoding is generally a character form, and for the picture, Files such as videos can only be used in bytes
- The use of the scene, in fact, the use of the scene needs to take two steps to consider, first of all to consider the obvious scenarios, such as the operation of the file or string operation, or pipeline communication, the next need to consider performance, such as the write operations of the file will be more frequent, if so, it is recommended to encapsulate it through BufferedWriter, Because file opening and writing to a small amount of data every time is a very inefficient way
Summarize
This article mainly on the basic IO class to do a simple comb, and on the basic concept of IO and how to use the IO base Class library to do the description, of course, this article does not list all the IO implementation class, interested students can view the Java document or source code, and this article does not customize the IO class library, In fact, the basic library provided by Java is sufficient to meet the requirements, and in addition, Java introduced NIO after 1.4, namely: No Blocking io, which differs from the original IO usage scenario.
Java Learning Exchange QQ Group: 589809992 prohibit small talk, non-happy do not enter!
Getting Started with Java IO