1. Streaming IO structure
The class diagram structure of the more commonly used IO stream is given.
Java IO Stream is divided into two main categories: byte stream and character stream, byte stream is based on InputStream and OutputStream, character stream is based on reader and writer.
2. Byte stream
Byte streams are based on InputStream and OutputStream, and the commonly used subclass streams are:
FileInputStream and FileOutputStream are used to read and write binary data from a file;
FilterInputStream and Filteroutputstream are filtered streams, which can nest basic binary input and output streams or other filter streams, enhancing processing power for processing input and output streams. Where the Bufferedinputstream and Bufferedoutputstream streams provide read/write buffering capabilities, DataInputStream and DataOutputStream provide more support for byte stream reading and writing (e.g., reading and writing binary integers, etc.);
ObjectInputStream and ObjectOutputStream are used to read and write objects, the class to which the object belongs needs to implement the serializable interface, and now the more common way is to use JSON format to pass objects;
Read-write binaries typically use the following nested streams:
New DataInputStream (New Bufferedinputstream (new FileInputStream (filename)));
New DataOutputStream (New Bufferedoutputstream (new FileOutputStream (filename)));
3. Character Stream
Character streams are based on reader and writer, and commonly used subclasses are:
FileReader and FileWriter classes are used to read and write text data from a file;
BufferedReader and Bufferwriter are used to read and write text data, providing read/write buffering functions, usually used to wrap other character input and output streams;
The common approach in the BufferedReader class is to read a row of data readline and then handle it using the related API of the String class, and the more common way is to use the Java.util.Scanner class to provide more support for the read character stream;
The PrintWriter class provides more support for writing streams;
Read-write text files typically use nested streams as follows:
New BufferedReader New FileReader (New File (...));
New PrintWriter (New BufferedWriter (New FileWriter (New File (...)));
4. Conversion of Byte stream and character stream
The byte stream is replaced by a character flow using InputStreamReader:
New BufferedReader (New InputStreamReader (InputStream));
Convert character streams to byte stream using OutputStreamWriter:
New BufferedWriter (New OutputStreamWriter (OutputStream));
5. zip file input/output stream
Java provides read and write support for zip compressed files, related classes are in the Java.util.zip package.
The Zipinputstream class and the Zipoutputstream are inherited from the Java.io.FilterInputStream class and the Java.io.FilterOutputStream class, respectively, for reading and writing zip compressed files.
The ZipEntry class is used to represent an entry in a zip file, read and write a zip file, first create a zipinputstream or Zipoutputstream, and then use the Getnextentry method to get each compressed file in the zip file, processed sequentially, After each processing completes an entry, call the Closeentry method to close the current entry, and after processing the entire ZIP file, call the Close method to close the entire zip file.
Here is an example that prints the contents of all the files in a zip file sequentially:
New Zipinputstream (new FileInputStream ("Test.zip")); ZipEntry ZipEntry; Scanner Scanner; while NULL ) { new Scanner (Zin); System.out.println (Zipentry.getname ()); // output file name // output file contents while (Scanner.hasnextline ()) { System.out.println (Scanner.nextline ()); } Zin.closeentry ();} Zin.close ();
Java Streaming IO