当前位置:网站首页>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
边栏推荐
- Setting up the development environment of dataworks custom function
- [solved] unknown error 1146
- Hash table, generic
- PAT甲级真题1166
- Some experiences of Arduino soft serial port communication
- File operation serialization recursive copy
- Download address collection of various versions of devaexpress
- Topic | synchronous asynchronous
- How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
- TCP cumulative acknowledgement and window value update
猜你喜欢

Inno setup production and installation package

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log

【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘

Deep learning parameter initialization (I) Xavier initialization with code

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

Summary of abnormal mechanism of interview

JUC forkjoinpool branch merge framework - work theft

docker建立mysql:5.7版本指定路径挂载不上。

最全SQL与NoSQL优缺点对比

TreeMap
随机推荐
php安装swoole扩展
JUC forkjoinpool branch merge framework - work theft
Distributed lock
[solved] win10 cannot find a solution to the local group policy editor
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
Distributed ID
C code production YUV420 planar format file
Jeecg data button permission settings
Upgrade CentOS php7.2.24 to php7.3
Advanced API (multithreading 02)
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
VMWare网络模式-桥接,Host-Only,NAT网络
La différence entre le let Typescript et le Var
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
20220319
[most detailed] latest and complete redis interview book (50)
Use of framework
GStreamer ffmpeg avdec decoded data flow analysis
IPv4 address
sharepoint 2007 versions