当前位置:网站首页>Objectinputstream read file object objectoutputstream write file object
Objectinputstream read file object objectoutputstream write file object
2022-06-11 12:03:00 【java-zh】
Test object
public class User implements Serializable {
Long serialVersionUID = 1L;
private Long userId;
private String userName;
private String password;
public User(Long userId, String userName, String password) {
this.userId = userId;
this.userName = userName;
this.password = password;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}Be careful : Be sure to implement serialization and deserialization Realization Serializable Interface is to serialize objects , The essence of serialization is to turn an object into a stream . Defined serialVersionUID Can be deserialized , It doesn't matter. , But there must be this attribute
Write file object
public class ObjectOutputStreamDemo {
public static void main(String[] args) {
File file = new File("d:\\classTest.txt");
ObjectOutputStream objectOutputStream = null;
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
objectOutputStream = new ObjectOutputStream(outputStream);
//objectOutputStream.writeUTF("www.baidu.com");
objectOutputStream.writeObject(new User(1L,"zhong","123456"));
objectOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}Read file object
public class ObjectInputStreamDemo {
public static void main(String[] args) {
File file = new File("d:\\classTest.txt");
InputStream inputStream = null;
ObjectInputStream objectInputStream = null;
try {
inputStream = new FileInputStream(file);
objectInputStream = new ObjectInputStream(inputStream);
//System.out.println(objectInputStream.readUTF());
Object o = objectInputStream.readObject();
if(o instanceof User){
User user = (User) o;
System.out.println(user);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}边栏推荐
- Elk - hearthbeat implements service monitoring
- iframe 传值
- 【LeetCode】494. Objective and (2 wrong questions)
- What is the latest popular annuity insurance product with higher income in 202?
- arguments.callee 实现函数递归调用
- yapi安装
- Zero after factorial (C language)
- Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
- Notes on brushing questions (13) -- binary tree: traversal of the first, middle and last order (review)
- 带你了解直接插入排序(C语言)
猜你喜欢

Linux忘记MySQL密码后修改密码

.net core 抛异常对性能影响的求证之路

Solve the problem of swagger document interface 404

Use of Chinese input method input event composition

安全工程师发现PS主机重大漏洞 用光盘能在系统中执行任意代码

Read geo expression matrix

Software project management 7.1 Basic concept of project schedule
![[JUC supplementary] immutable object, shared meta mode, final principle](/img/c1/c29229108a3f66b83d13b4d90d49f7.jpg)
[JUC supplementary] immutable object, shared meta mode, final principle

01_ Description object_ Class diagram

Use compiler option '--downleveliteration' to allow iteration of iterations
随机推荐
阶乘后的零(C语言)
Use compiler option '--downleveliteration' to allow iteration of iterations
MYCAT sub database and sub table
Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
iframe 传值
带你了解直接插入排序(C语言)
[go] interpretation of gin source code
Uncaught TypeError: Cannot set property ‘next‘ of undefined 报错解决
POJ 3278 catch the cow (width first search, queue implementation)
Software project management 7.1 Basic concept of project schedule
C # apply many different fonts in PDF documents
Where is it safer to open an account for soda ash futures? How much capital is needed for a transaction?
Let you understand selection sorting (C language)
Golang uses XOR ^ to exchange two variables and encrypt / decrypt them
2019年书单
golang利用异或^交换两个变量以及加解密
[file upload vulnerability 05] server suffix detection and bypass experiment (based on upload-labs-3 shooting range)
Maximum water container
Is the SSL certificate reliable in ensuring the information security of the website?
导师转我800块,让我仿真一个电路(电源设计)