Java basics: other classes in the I/O package
1. Object stream (ObjectInputStream/ObjectOutputStream) 1. Overview
Store the objects in heap memory to the hard disk and keep the data in the objects, which is called object persistence (or serialization ). Two classes used: ObjectInputStream and ObjectOutputStream
Serializable must be implemented for the operated object
2. Features
Object serialization and deserialization. ----------- WriteObject readObject
Serializable flag interface ------------------ used to give the ID of the serialized class home
Keywords: transient ------------------------ transient, static and transient objects are not written
Transient: This keyword can be used to modify non-static data that does not want to be serialized.
3. Example:
Public static void readObj () throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream (new FileInputStream (obj. object); // deserialization of objects. Person p = (Person) ois. readObject (); System. out. println (p. getName () +: + p. getAge (); ois. close ();} public static void writeObj () throws IOException, IOException {ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (obj. object); // serialize the object. The serialized object must implement the Serializable interface. Oos. writeObject (new Person (Xiaoqiang, 30); oos. close ();}
Ii. Random access to files using RandomAccessFile: I. Overview RandomAccessFile instances support reading and writing random access files. They have their own read and write methods.
2. features:
** It can be read and written.
** A large byte array is maintained internally to read and write data to the array.
** You can use the getFilePointer method to obtain the pointer position. You can also use the seek method to set the pointer position.
** The object content should encapsulate the byte input stream and the byte output stream.
** This object can only operate on files.
You can use the seek method to read and write data from any position in the array.
You can modify the data.
Note: Data must be regular.
3. Example:
// Define a random access file, which can be read and written into RandomAccessFile raf = new RandomAccessFile(ranacc.txt, rw); raf. write (Michael. getBytes (); // write content raf. writeInt (97); // set the pointer position through seek. RandomAccessFile raf = new RandomAccessFile(ranacc.txt, r); raf. seek (1*8); // random read. You only need to specify the pointer position. Byte [] buf = new byte [4]; raf. read (buf );
3. Pipeline stream: 1. Overview
Pipeline streams include PipedOutputStream and PipedInputStream, which must be combined with multithreading technology. Do you still remember the multi-thread knowledge. Let's look at the example to recall.
2. Example
4. DataInputStream and ByteArrayInputStream
1. objects that operate on basic data type values.
DataInputStream
DataOutputStream
These two read/write objects can be used to operate stream objects of basic data types, including methods for reading and writing various basic data types.
2. the device is the stream object of the memory.
ByteArrayInputStream ByteArrayOutputStream
CharArrayReader CharArrayWriter
A. This object does not call the underlying resources, so you do not need to close the stream resources. Even if it is disabled, it can still be called.
B. The buffer is contained inside, which is equivalent to using the memory as the stream operation source and destination without any IO exceptions.
C. Write a number into another array (all in memory)