当前位置:网站首页>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());
}
}
}
边栏推荐
- [azure data platform] ETL tool (7) - detailed explanation of ADF copy data
- Redis memory optimization and distributed locking
- Data from the first to seventh census (to counties)
- 在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
- Differences of several query methods in PDO
- Azure SQL db/dw series (10) -- re understanding the query store (3) -- configuring the query store
- Economic panel topic 1: panel data of all districts and counties in China - more than 70 indicators such as population, pollution and agriculture (2000-2019)
- Three ways of scala string interpolation
- Time processing class in PHP
- To resolve project conflicts, first-class project managers do so
猜你喜欢

Get to know druid IO real time OLAP data analysis storage system

PostgreSQL common SQL

Druid query

Parallel one degree relation query

Window and various windowfunctions in Flink

Spark optimization - data skew solution

Patrick Pichette, partner of inovia, former chief financial officer of Google and current chairman of twitter, joined the board of directors of neo4j

Azure SQL db/dw series (12) -- using query store (1) -- report Introduction (1)

ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
![[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)](/img/5d/1fc04b4384af07b953c99ec37383b3.jpg)
[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
随机推荐
Three ways of scala string interpolation
Loading process of [JVM series 3] classes
Spark core concepts: Master, worker, driver program, executor, RDDS
Two Chinese vector map data with map review number
Scala sets (array, list, set, map, tuple, option)
Graph data modeling tool
[azure data platform] ETL tool (7) - detailed explanation of ADF copy data
Time processing class in PHP
[JVM series 8] overview of JVM knowledge points
测试写入mysql数据300W条,每次1.6-2W之间就断掉然后出现以下问题:
Alibaba cloud OSS access notes
MySQL learning summary XIII: detailed explanation of if, case, loop, while & cursor of process control
Parallel one degree relation query
[figure data] how long does it take for the equity network to penetrate 1000 layers?
C language programming -- input a string (including letters, numbers, punctuation marks, and space characters) from the keyboard, calculate the actual number of characters and print out, that is, it d
MySQL auto sort function deny_ rank() over()、rank() over()、row_ Num() over() usage and differences
PHP import classes in namespace
【同步功能】2.0.16-19 版本都有同步功能修复的更新,但未解决问题
Reading notes of effective managers
Neo4j auradb free, the world's leading map database