当前位置:网站首页>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
边栏推荐
- Win 10 find the port and close the port
- 2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- IPv4 address
- 10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
- 树莓派更新工具链
- Mise en place d'un environnement de développement de fonctions personnalisées
- MySQL mistakenly deleted the root account and failed to log in
- Thoughts on project development
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
- PAT甲级真题1166
猜你喜欢

带你全流程,全方位的了解属于测试的软件事故

3311. 最长算术

Arduino Serial系列函数 有关print read 的总结

Summary of Arduino serial functions related to print read

dataworks自定義函數開發環境搭建

JUC forkjoinpool branch merge framework - work theft

Circuit, packet and message exchange
![Gridome + strapi + vercel + PM2 deployment case of [static site (3)]](/img/65/8d79998e96a2c74ba6e237bee652c6.jpg)
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]

Hash table, generic

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
随机推荐
[HCAI] learning summary OSI model
Advanced API (use of file class)
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Margin left: -100% understanding in the Grail layout
带你全流程,全方位的了解属于测试的软件事故
Raspberry pie update tool chain
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
JS date comparison
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
Split small interface
docket
I. D3.js hello world
TCP cumulative acknowledgement and window value update
3311. 最长算术
File operation serialization recursive copy
Advanced API (character stream & net for beginners)
Basic components and intermediate components
4279. 笛卡尔树
Advanced API (batch image Download & socket dialog)