当前位置:网站首页>Use of other streams
Use of other streams
2022-07-03 07:21:00 【four thousand three hundred and forty-three】
I / O stream
Data flow :
/*
Data flow
DataInputStream and DataOutputStream
effect : Used for basic data type traversal or string reading and writing
*/
@Test
public void test1() throws IOException {
// Write operation
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF(" Zhang pock ");
dos.flush();
dos.writeInt(40);
dos.flush();
dos.writeBoolean(true);
dos.flush();
dos.close();
// Got data.txt The file cannot be read directly
}
@Test
// Read the variables in the file into memory
public void test2() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
// The reading order should be consistent with the order when writing the file
String name = dis.readUTF();
int age = dis.readInt();
boolean b = dis.readBoolean();
System.out.println(name+age+b);
dis.close();
}
Object flow :
serialize : Write to file
Deserialization : Write from file to memory
package ObjectInputOutputStream;
import org.junit.Test;
import java.io.*;
public class ObjectStreamTest {
/*
Put... In memory java Objects are saved to disk or transmitted over the network
Use ObjectOutputStream Realization
*/
@Test
public void test() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ObjectStream.txt"));
oos.writeObject(new String(" I love China !"));
oos.flush();
oos.close();
}
/*
Deserialization : Put the disk file ( The Internet ) Objects in are restored to objects in memory
Use ObjectInputStream
*/
@Test
public void test2() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ObjectStream.txt"));
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}
}
Class serialization has certain requirements : One of the following two interfaces must be implemented
static,transient The variable of cannot be serialized
If it is not added manually serialVersionUID , After modifying the object ,JVM Of UID The corresponding changes will result in failure to deserialize
边栏推荐
- Mise en place d'un environnement de développement de fonctions personnalisées
- 《指环王:力量之戒》新剧照 力量之戒铸造者亮相
- Discussion on some problems of array
- How to specify the execution order for multiple global exception handling classes
- [day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
- Distributed transactions
- 7.2 brush two questions
- Advanced APL (realize group chat room)
- LeetCode
- TypeScript let與var的區別
猜你喜欢
Common problems in io streams
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Pat grade a real problem 1166
Distributed transactions
Pits encountered in the use of El checkbox group
3311. Longest arithmetic
TreeMap
Wireshark software usage
7.2刷题两个
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
随机推荐
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
4279. Cartesian tree
Advanced API (use of file class)
Advanced APL (realize group chat room)
Win 10 find the port and close the port
【无标题】
Specified interval inversion in the linked list
Use of framework
dataworks自定義函數開發環境搭建
Arduino 软串口通信 的几点体会
Advanced API (multithreading 02)
深度学习参数初始化(一)Xavier初始化 含代码
Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
SecureCRT取消Session记录的密码
Basic components and intermediate components
MySQL transaction rollback, error points record
Arduino Serial系列函数 有关print read 的总结
How to specify the execution order for multiple global exception handling classes
萬卷書 - 價值投資者指南 [The Education of a Value Investor]