当前位置:网站首页>serialization and deserialization
serialization and deserialization
2022-06-12 01:47:00 【aigo-2021】
One 、 Serialization and deserialization
After the object is serialized , It can be used for network transmission or disk storage .
The essence is , Serialization is to turn an object into byte[ ] form ; Deserialization Will be byte[ ] Become the object .
Object should be serialized , Class must implement Serializable Interface
ObjectInputStream and ObjectOutputStream
Direct read / write object , The premise is that the class implementation of the object Serializable Interface
public static void main(String[] args) {
// Will a Person Object store to temp.txt in
Person p = new Person("tom",20);
try {
FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113/temp.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} // Read temp.txt Objects stored in the
try {
FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113/temp.txt");
ObjectInputStream bis = new ObjectInputStream(fis);
Person p = (Person)bis.readObject();
System.out.println(p.getName()+","+p.getAge());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Two 、 Object to serialize and deserialize
Get the object of byte[ ] And take byte[ ] Become the object ; Encapsulating two methods
Need to use ByteArrayInputStream and ByteArrayOutputStream , It's right byte[ ] Read / write stream of
public class Test {
public static void main(String[] args) {
Person p = new Person("tom",22);
Person p2 = new Person("jack",21);
Person p3 = new Person("smith",20);
List<Person> list = new ArrayList<Person>();
list.add(p);
list.add(p2);
list.add(p3);
byte[] bytes = getBytes(list);
List<Person> ps = (List<Person>)getObject(bytes);
for(Person person:ps) {
System.out.println(person.getName()+","+person.getAge());
}
}
/**
* Sequence objects into byte[]
* @param obj
* @return
*/
public static byte[] getBytes(Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* take byte[] Anti serialization into objects
* @param bytes
* @return
*/
public static Object getObject(byte[] bytes) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
3、 ... and 、Properties Tool class
For reading Properties The configuration file A tool class of ,Java The configuration file in includes .properties and .xml Configuration file in the form , properties Writing and reading configuration files is relatively simple , In order to key=value In the format of , But it can not represent the structure of data ; xml Formal Configuration file writing and reading relative replication , Represents the structure of data
Properties Inherited from Hashtable ,Properties The configuration information will be read to Hashtable in , adopt key get value Programming to read the configuration file
public class TestProperties {
public static void main(String[] args) {
Properties p = new Properties();
try {
// Read the contents of the configuration file into Properties Of Hashtable in
FileInputStream fis = new
FileInputStream("D:\\MyWork\\MyWorkspaces\\eclipse0106\\pjt0211\\src\\test.properties");
p.load(fis);
System.out.println(p.getProperty("username"));
System.out.println(p.getProperty("password"));
// towards Hashtable Set content in
p.setProperty("sfz", "1001100");
p.store(new
FileOutputStream("D:\\MyWork\\MyWorkspaces\\eclipse0106\\pjt0211\\src\\test.properties"), " Is it a note ");
System.out.println(p.getProperty("sfz"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// Class path (classPath)
ResourceBundle bundle = ResourceBundle.getBundle("test");
System.out.println(bundle.getString("username"));
}边栏推荐
- Agile v.s. Waterfall
- Point cloud perception algorithm interview knowledge points (II)
- Make ads more relevant by searching for additional information about ads
- [project training] wechat official account to obtain user openid
- 2022年金属非金属矿山(小型露天采石场)安全管理人员考试模拟100题及模拟考试
- Huawei intermodal game or application review rejected: the application detected payment servicecatalog:x6
- Simulated 100 questions and simulated examination for safety management personnel of metal and nonmetal mines (small open pit quarries) in 2022
- 自适应搜索广告有哪些优势?
- Tabulation v.s. Recursion With Memory (Dynamic Programming)
- 小程序111111
猜你喜欢

如何让杀毒软件停止屏蔽某个网页?以GDATA为例

Introduction to prism framework - Modular introduction

MySQL实训报告【带源码】

What are the advantages of adaptive search advertising?

如何为Excel中的单元格自动填充颜色

博文推荐|BookKeeper - Apache Pulsar 高可用 / 强一致 / 低延迟的存储实现

Bracket generation (backtracking)

Tiobe - programming language ranking in June 2022

Huawei, this is too strong

“還是學一門技術更保險!”杭州校區小哥哥轉行軟件測試,喜提10K+雙休!
随机推荐
如何提高广告的广告评级,也就是质量得分?
Wide match modifier symbol has been deprecated, do not use
Installing mysql-5.7 for Linux (centos7)
联调这夜,我把同事打了...
php开发09 文章模块的删除和文章分类编写
括号生成(回溯)
“还是学一门技术更保险!”杭州校区小哥哥转行软件测试,喜提10K+双休!
Linux(CentOS7)安裝MySQL-5.7版本
Sogou Pinyin official website screenshot tool pycharm installation
Redis cluster + sentinel mode + replicas
Operation of simulated examination platform of diazotization process examination question bank in 2022
Three main factors determining advertising quality
Leetcode 55 jump game
关于vagrant up的一个终结之谜
商城开发知识点
如何为Excel中的单元格自动填充颜色
Freshman year: learning summary
2022年金属非金属矿山(小型露天采石场)安全管理人员考试模拟100题及模拟考试
Markov networks and conditional random fields
Spiral matrix (skill)