当前位置:网站首页>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
边栏推荐
- dataworks自定义函数开发环境搭建
- [untitled]
- CentOS php7.3 installing redis extensions
- [most detailed] latest and complete redis interview book (50)
- When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
- Pits encountered in the use of El checkbox group
- Deep learning parameter initialization (I) Xavier initialization with code
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
- Distributed transactions
- VMWare网络模式-桥接,Host-Only,NAT网络
猜你喜欢

3311. Longest arithmetic

File operation serialization recursive copy

Basic knowledge about SQL database
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

Pits encountered in the use of El checkbox group

Spa single page application

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef

Map interface and method

Discussion on some problems of array
随机推荐
Store WordPress media content on 4everland to complete decentralized storage
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
Laravel Web Framework
Advanced API (local simulation download file)
Laravel frame step pit (I)
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
"Moss ma not found" solution
691. Cube IV
3311. Longest arithmetic
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
SecureCRT取消Session记录的密码
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
Distributed ID
VMware virtual machine installation
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
sharepoint 2007 versions
II. D3.js draw a simple figure -- circle
High concurrency memory pool
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*