IO streams (lower)

First, the object flow:

① What is the object serialization mechanism:

  • Object serialization mechanism allows memory to convert platform-independent Java object into a binary stream, thereby allowing the binary stream which is permanently stored on a disk, such a binary, or streaming over a network to another network node. // get this program when other binary stream can be restored to the original Java objects

 

  • Serialization advantage is that any object that implements Serializable interface be converted to bytes of data, it can be saved and restored during transmission

 

 

  • Serialization is RMI (Remote Method Invoke - Remote Method Invocation) mechanism of the process parameters and return values ​​must achieve, and RMI is the foundation of JavaEE. Therefore serialization mechanism is the basis of platform JavaEE

 

 

  • If you need to make an object supports serialization mechanism, you must let the class and its properties object belongs is serializable, in order to allow a class is serialized, the class must implement one of the following two interfaces. Otherwise, an exception will be thrown NotSerializableException

 

  1. Serializable

  2. Externalizable

  • Those who achieve Serializable interface class has a static variable represents the serialized version of the identifier:
  1. private static final long serialVersionUID;

  2. serialVersionUID used to indicate the compatibility between different versions of the class. In short, its purpose is to serialize objects version control is compatible when deserializing about each version.

  3. If the classes do not define the static constant whose value is the internal details of the Java Runtime Environment according to the class automatically generated. If the class instance variables has been modified, serialVersionUID may change. It is suggested, explicitly declared.

  4.  In short, Java serialization mechanism is by determining the class at run-time version serialVersionUID to verify consistency. During deserialization, JVM will came byte stream serialVersionUID serialVersionUID with the respective local entity class are compared, it is considered to be consistent if the same can be deserialized, otherwise there will be a serialized version inconsistent exception. (InvalidCastException)

 

② serialization and de-serialization of usage:

  • If a class implements Serializable interface, object of this class is serializable:
  1. Create an ObjectOutputStream

  2. Calling ObjectOutputStream object writeObject (Object) method to output serializable objects

  3. Write a note, operating flush () once

  • Deserialization
  1. Create an ObjectInputStream

  2. Call readObject () method reads the object stream

  • He stressed: If a class attribute is not a primitive data types or String type, but another reference type , then the reference types must be serialized, or else have this type of Field classes can not be serialized. And the same can not be modified sequence of static and transient member variables.

 

Second, a random access file stream:

①RandomAccessFile use:

  1. RandomAccessFile directly inherited from java.lang.Object class that implements DataInput and DataOutput interface.

  2. RandomAccessFile either as an input stream, but also as an output stream.

  3. If RandomAccessFile as an output stream, write to the file if not present, is automatically created in the implementation process, if written out to the file exists, it will overwrite the original file contents. (By default, covering from the beginning)

  4. Related operations can achieve a RandomAccessFile "insert" data effect. (Primarily seek method acts as a pointer, parameter specified angular positioning target position).

②RandomAccessFile structure:

  • Constructor
  1. public RandomAccessFile(File file, String mode)

  2. public RandomAccessFile(String name, String mode)

  • Create a class instance RandomAccessFile need to specify a mode parameter that specifies the access mode of RandomAccessFile:
  1. r: Open Read Only

  2. rw: open for reading and writing

  3. rwd: Open for reading and writing; update the contents of the file synchronization

  4. rws: open for reading and writing; synchronization update file content and metadata

  • If the read-only mode r. File is not created, but will go to read the file already exists, if the file is read does not exist, an exception occurs. If the mode is rw read and write. If the file does not exist will be to create a file, if there is not created.

 

Guess you like

Origin www.cnblogs.com/liuhuan425/p/10948911.html