Data operation flow
In the io package, two platform-independent data operation streams are provided:
DataInputStream
DataOutputStream
Usually the data output stream will output the data in a certain format, and then read the data in a certain format through the data input stream.
The DataOutputStream interface defines a series of writeXxx() operations, which can write data of various data types.
Example: Write and read data using data manipulation streams
import java.io.DataOutputStream ; import java.io.File ; import java.io.FileOutputStream ; public class DataOutputStreamDemo{ public static void main(String args[]) throws Exception{ // All exceptions throw DataOutputStream dos = null ; // Declare the data output stream object File f = new File("d:" + File.separator + "order.txt"); // The file's save path dos = new DataOutputStream(new FileOutputStream(f)) ; // Instantiated data output stream object String names[] = {"Shirt","Glove","Scarf"} ; // Product name float prices[] = {98.3f,30.3f,50.5f} ; // Product price int nums[] = {3,2,1} ; // Product quantity for(int i=0;i<names.length;i++){ // Loop output dos.writeChars(names[i]) ; // Write string dos.writeChar('/t') ; // Write separator dos.writeFloat(prices[i]) ; // Write price dos.writeChar('/t'); // Write delimiter dos.writeInt(nums[i]); // Write quantity dos.writeChar('/n'); // Newline} dos.close(); // Close output stream} }; import java.io.DataInputStream ; import java.io.File ; import java.io.FileInputStream ; public class DataInputStreamDemo{ public static void main(String args[]) throws Exception{ // All exceptions throw DataInputStream dis = null ; // Declare the data input stream object File f = new File("d:" + File.separator + "order.txt"); // The file's saving path dis = new DataInputStream(new FileInputStream(f)) ; // Instantiate the data input stream object String name = null ; // Receive name float price = 0.0f ; // Receive price int num = 0 ; // Receive quantity char temp[] = null ; // Receive product name int len = 0 ; // Save the number of read data char c = 0 ; // '/u0000' try{ while(true){ temp = new char[200] ; // Open space len = 0 ; while((c=dis.readChar())!='/t'){ // Receive content temp[len] = c ; len ++ ; // Read length plus 1 } name = new String(temp,0,len) ; // Change the character array to String price = dis.readFloat() ; // Read price dis.readChar() ; // Read /t num = dis.readInt() ; // Read int dis.readChar() ; // Read int dis.readChar() ; // Read /n System.out.printf("name: %s; price: %5.2f; quantity: %d/n",name,price,num) ; } }catch(Exception e){} dis.close() ; } };Object serialization
Object serialization is a method of turning objects into binary data streams. Through object serialization, the transmission or storage of objects can be easily realized.
If a class wants to support initialization, the class must implement the java.io.Serilizable interface. The interface is defined as follows:
publicinterfaceSerilizable{}
There is no method in this interface, so this class belongs to a label interface, indicating that the class implementing the interface has some capabilities.
1. Serialization and deserialization of objects
2.serialVersionUID
A serialVersionUID constant is introduced in the serialization operation. This constant can be used to verify the consistency of the version. When deserializing, the JVM will compare the serialVersionUID in the passed in byte stream with the serialVersionUID of the local corresponding class. If the same is considered to be consistent, it can be deserialized, otherwise an exception with inconsistent serialization versions will occur.
import java.io.Serializable ; public class Person implementsSerializable{ private String name ; // Declare the name attribute, but this attribute is not serialized private int age ; // Declare the age attribute publicPerson(String name,int age){ // Set content by constructing this.name= name ; this.age= age ; } publicString toString(){ // Overwrite the toString() method to return "name: " + this.name + "; Age: " + this.age; } }; 3. Object output stream: ObjectOutputStream
If an object wants to output, it must use the ObjectOutputStream class, which is defined as follows
If an attribute in an object does not want to be serialized, it can be declared using the transient keyword.
importjava.io.File ; importjava.io.FileOutputStream ; importjava.io.OutputStream ; importjava.io.ObjectOutputStream ; publicclass SerDemo01{ public static void main(String args[]) throwsException { File f = new File("D:" +File.separator + "test.txt") ; //Define the save path ObjectOutputStream oos = null ; // Declare the object output stream OutputStream out = newFileOutputStream(f); // File output stream oos = new ObjectOutputStream(out); oos.writeObject(new Person("Zhang San",30)); // Save the object oos.close(); // Close } };4. Object Input Stream: ObjectInputStream
importjava.io.File ; importjava.io.FileInputStream ; importjava.io.InputStream ; importjava.io.ObjectInputStream ; publicclass SerDemo02{ public static void main(String args[]) throwsException { File f = new File("D:" +File.separator + "test.txt") ; //Define the save path ObjectInputStream ois = null ; // Declare the object input stream InputStream input = newFileInputStream(f); // File input stream ois = new ObjectInputStream(input); // Instantiate object input stream Object obj = ois.readObject(); // Read object ois.close(); // Close System.out.println(obj); } };6. Serialize a group of objects
When outputting an object, only one object output operation is provided (writeObject(Objectobj)) and no multiple objects are provided. If you want to serialize multiple objects now, you can use the object array to complete it. Since the array is a reference data type, you can directly use the Object type to receive it.
importjava.io.File ; importjava.io.IOException ; importjava.io.FileOutputStream ; importjava.io.OutputStream ; importjava.io.ObjectOutputStream ; importjava.io.FileInputStream ; importjava.io.InputStream ; importjava.io.ObjectInputStream ; publicclass SerDemo05{ public static void main(String args[]) throwsException{ Person per[] = {new Person("Zhang San",30),newPerson("Li Si",31), new Person("Wang Wu",32)}; ser(per) ; Object o[] = (Object[])dser() ; for(int i=0;i<o.length;i++){ Person p = (Person)o[i] ; System.out.println(p) ; } } public static void ser(Object obj[]) throwsException { File f = new File("D:" +File.separator + "test.txt") ; //Define the save path ObjectOutputStream oos = null ; //Declare the object output stream OutputStream out = new FileOutputStream(f); //File output stream oos = new ObjectOutputStream(out) ; oos.writeObject(obj) ; //Save object oos.close() ; //Close} public static Object[] dser() throws Exception{ File f = new File("D:" +File.separator + "test.txt") ; //Define the save path ObjectInputStream ois = null ; //Declare the object input stream InputStream input = newFileInputStream(f); //File input stream ois = new ObjectInputStream(input); //Instantiate the object input stream Object obj[] =(Object[])ois.readObject() ; //Read the object ois.close() ; //Close return obj ; } };The number of objects that an array can store is limited, so you can use class sets to perform serialization operations.
Compressed stream
In order to reduce the amount of data during transmission, compressed streams are also provided in Java, and files or folders can be compressed into ZIP, JAR, GZIP and other formats.
This stream is used less, so just a brief introduction.
Summarize
The above is all the content of this article about java IO data operation flow, object serialization, and compressed stream code analysis. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!