当前位置:网站首页>Serialization & deserialization

Serialization & deserialization

2022-06-13 03:35:00 Mouth strong programmer

-- Copy of documents and shear
        File file = new File("f:\\ picture \\0809.txt");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] bs = new byte[(int)file.length()];
        bis.read(bs);
        bis.close();
        fis.close();
        
        File newF = new File("f:\\0105.txt");
        FileOutputStream fos = new FileOutputStream(newF);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(bs);
        bos.close();
        fos.close();

        file.delete();
        System.out.println("ok");

Before, we could only operate on strings Today we are going to learn the advanced class of reading and writing
-- serialize and Deserialization
In practice , Data can be imported from the media of the hard disk You can also export from memory

// serialize : The process of storing an object from memory to hard disk
// All classes to be serialized must implement Serializable Interface
        Dog d=new Dog(" Laifu 1"," Male ",23);
        File file = new File("f:\\ picture \\dog1");
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(d);

// Closing must be from the inside out !
        oos.close();
        bos.close();
        fos.close();
        System.out.println("ok");
        
// Deserialization : Restore objects from hard disk media to memory
        File file = new File("f:\\ picture \\dog");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Dog d = (Dog)ois.readObject();
        System.out.println(d.getName());
        ois.close(); 
        bis.close();
        fis.close();

preservation 1000 A dog  
        ArrayList<Dog> dogs = new ArrayList<Dog>();
        for (int i = 0; i < 1000; i++) {
            Dog d = new Dog(" Wangcai "+i, " Male ", i+1);
            dogs.add(d);
        }
        
        File file = new File("f:\\ picture \\dogs.bak");
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(dogs);
        oos.close();
        bos.close();
        fos.close();
        System.out.println(" perfect ?");

Whether all classes that implement serialization must implement Serializable Interface ? that arrayList Collective pinch ?Dog Is there any need to ?

Serialization and deserialization

package com.zking.test2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Demo_01 {
	public static void main(String[] args) throws Exception{
		/**
		 *  Today's new lesson :
		 * 		1. The day before yesterday --- Read and write text 
		 * 			FileInputStream       ---BufferedInputStream
		 * 			FileOutputStream	  ---BufferedOutputStream
		 * 
		 * 		2. Today, --- in the light of Java The objects generated in the program are read and written 
		 * 			 Knowledge point : Serialization and deserialization 
		 * 
		 * 		3. serialize   ObjectOutputStream
		 * 			 Save the objects generated in memory to a text file 
		 * 			 Be careful : Entity classes must implement serialization interfaces 
		 * 
		 * 		4. Deserialization  ObjectInputStream
		 * 			 The contents of the local file ( object )-- Read to memory 
		 * 
		 * 		
		 */
		
		// serialize 
		// Prepare an object 
	     Student stu = new Student(1, " Huqing stick ");
//		// Output stream 
		File file = new File("D:\\stu.bak");
		FileOutputStream fos = new FileOutputStream(file);
//		// Write through serialized stream ObjectOutputStream
		ObjectOutputStream oos = new ObjectOutputStream(fos);
//		// Write an object through the serialization stream 
		oos.writeObject(stu);
//		// close 
		oos.close();
		fos.close();
		
		
		// Read objects in the file 
		
		File file = new File("D:\\stu.bak");
		FileInputStream fis= new FileInputStream(file);
//		// Into a deserialized stream 
		ObjectInputStream ois = new ObjectInputStream(fis);
//		// Read method 
		Student stu = (Student)ois.readObject();
		System.out.println(stu);
		ois.close();
		fis.close();
		
		
		
		// Define a list aggregate    preservation 100 A student      Write to file 
		List<Student> list = new ArrayList<Student>();
		for (int i = 1; i <= 100; i++) {
			list.add(new Student(1+i," Zhang San "+i));
		}
		
//		//File
		File file = new File("D:\\stu2.bak");
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos =new BufferedOutputStream(fos);
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(list);
		System.out.println(" Write successfully ");
		oos.close();
		bos.close();
		fos.close();
		
		// Read set 
		File file = new File("D:\\stu2.bak");
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		ObjectInputStream ois = new ObjectInputStream(bis);
		
		List<Student> list = (List<Student>)ois.readObject();
		// Iterators play with 
		Iterator<Student> iterator = list.iterator();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}

		
	}
}

原网站

版权声明
本文为[Mouth strong programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280529564382.html