Buffer stream & conversion stream & sequence commutation

preface

Java - IO stream Foundation

Buffer stream

  • Byte buffer stream: BufferedInputStream, BufferedOutputStream
  • Character buffer stream: BufferedReader, BufferedWriter

The basic principle of buffered stream is that when creating stream objects, a built-in buffer array of default size will be created to reduce the number of system IO through buffer reading and writing, so as to improve the efficiency of reading and writing. (usage is not much different from FileInputStream)

// Create byte buffered input stream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt"));
// Create byte buffered output stream
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
// Create character buffered input stream
BufferedReader br = new BufferedReader(new FileReader("br.txt"));
// Create character buffered output stream
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));

Conversion flow

Convert GBK encoded text file into UTF-8 encoded text file.

 // 1. Create a conversion input stream object, associate the data source file path, and specify the code as gbk
InputStreamReader isr = new InputStreamReader(new FileInputStream("day13\\ccc\\gbk.txt"),"gbk");
// 2. Create the conversion output stream object, associate the destination file path, and the pointing code is utf8
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day13\\ccc\\utf8.txt"),"utf8");

Convert the encoding type after reading and writing. Pay attention to closing the stream.

Serialized stream

Java provides a mechanism for object serialization. An object can be represented by a byte sequence, which contains the data of the object, the type of the object and the attributes stored in the object. After the byte sequence is written out to the file, it is equivalent to persisting the information of an object in the file.
On the contrary, the byte sequence can also be read back from the file, reconstruct the object and deserialize it. Object data, object type and data information stored in the object can be used to create objects in memory.

Object – > objectoutputstream serialization – > bytes
Byte – > objectoutputstream deserialization – > object

Serialization:

To serialize an object, two conditions must be met:

  1. This class must implement Java io. Serializable interface. Serializable is a tag interface
  2. Write out object methods
    public final void writeObject (Object obj): writes out the specified object.
// Create a serialized stream object and associate the destination file path
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day13\\ddd\\a.txt"));
// Write out the object P, and the class of the P object must implement the Serializable interface
oos.writeObject(p);

Deserialization

If we can find the class file of an object, we can deserialize it and call the ObjectInputStream method to read the object:
public final Object readObject(): reads an object.

// 1. Create a deserialization stream object and associate the data source file path
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day13\\ddd\\a.txt"));
// 2. Read an object
Object obj = ois.readObject();

Serialization considerations

  • This class must implement Java io. Serializable interface. Serializable is a tag interface. Classes that do not implement this interface will not serialize or deserialize any state, and will throw NotSerializableException.
  • All properties of this class must be serializable.
  • If an attribute does not need to be serializable, the attribute must be marked as transient and modified with the transient keyword.

Considerations for deserialization

  • For a JVM to deserialize an object, it must be a class that can find a class file. If the class file of this class cannot be found, a ClassNotFoundException exception is thrown.
  • In addition, when the JVM deserializes the object, the class file can be found, but if the class file is modified after serializing the object, the deserialization operation will also fail and an InvalidClassException will be thrown. The reasons for this exception are as follows:
    • The serial version number of the class does not match the version number of the class descriptor read from the stream
    • The class contains an unknown data type
    • This class has no accessible parameterless constructor
      The Serializable interface provides a serial version number to the class to be serialized. serialVersionUID this version number is used to verify whether the serialized object and the corresponding class match.

Print stream

We usually print the output on the console by calling the print method and println method, both of which are from Java io. Printstream class, which can easily print values of various data types, is a convenient output method.

Construction method:
public PrintStream(String fileName): creates a new print stream with the specified file name.

// Create a print stream object and associate the destination file path
        PrintStream ps = new PrintStream("day13\\eee\\a.txt");
        // print data
        ps.print(100);
        ps.println();// Line feed
        ps.println("jack");

        // Close the flow and release resources
        ps.close();
  // Get the print stream object of the system:
        System.out.println(100);// Print to console 100
        // Requirements: system out. Println() change the destination of printing from the console to day13\eee\b.txt
        PrintStream ps2 = System.out;
        ps2.println(100);// Print to console 100
// Modify the print stream object of the system:
        // Create a print stream object and associate the destination file path
        PrintStream ps3 = new PrintStream("day13\\eee\\b.txt");
        // Modify system Value of out
        System.setOut(ps3);
        System.out.println(100);// Print in b.txt file

Commons IO Toolkit

Commons IO is a group of class libraries related to IO operations provided by apache open source foundation, which can greatly improve the efficiency of IO function development. The Commons IO toolkit provides many classes about IO operations,

  1. public static int copy(InputStream in, OutputStream out); Copy the contents in the input stream to the output stream, and return the number of copied bytes (suitable for file sizes below 2GB)
  2. public static long copyLarge(InputStream in, OutputStream out); Copy the contents in the input stream to the output stream, and return the number of copied bytes (suitable for file size above 2GB)
 private static void method01() throws IOException {
        // Copying a file 
        // Create input and output stream objects
        FileInputStream fis = new FileInputStream("day13\\aaa\\hb.jpg");
        FileOutputStream fos = new FileOutputStream("day13\\fff\\hb1.jpg");
        // Copy
        IOUtils.copy(fis,fos);
        // Close the flow and release resources
        fos.close();
        fis.close();
    }

Keywords: Java Back-end

Added by dnice on Sun, 13 Feb 2022 13:05:39 +0200