Java操作IO对象流进行数据的读写
作者:幻影寒狼 发布时间:2021-08-25 03:33:49
标签:Java,IO流,数据读写
对象的读写
使用ObjectInputStream和ObjectOutputStream读写对象(序列化与反序列化)。
只有字节流没有字符流
.类必须实现Serializable接口
给类加个序列化编号,给类定义一个标记,新的修改后的类还可以操作曾经序列化的对象
静态是不能被序列化的,序列化只能对堆中的进行序列化 ,不能对“方法区”中的进行序列化
不需要序列化的字段前加 transient
小例子:
先创建一个Dog对象并序列化:
package com.uwo9.test03;
import java.io.Serializable;
public class Dog implements Serializable {
private static final long serialVersionUID = 2809685095868158625L;
String name;
String color;
}
再创建一个Student对象并序列化:
package com.uwo9.test03;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 9078616504949971001L;
static public String schoolName;
private transient String name;
private transient int age;
private double score;
private Dog dog;
public Student() {
super();
}
public Student(String name, int age, double score, Dog dog) {
super();
this.name = name;
this.age = age;
this.score = score;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
将数据写入对象流并存入文件
package com.uwo9.test03;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
public class Test01 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "大黄";
dog.color = "Yellow";
Student student1 = new Student("学生1", 18, 99,dog);
Student student2 = new Student("学生2", 19, 99,dog);
Student student3 = new Student("学生3", 20, 99,dog);
Student.schoolName = "某某大学";
File file = new File("E:/Temp/Test1.txt");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
//oos.writeObject(student);
ArrayList<Student> arrayList = new ArrayList<>();
Collections.addAll(arrayList, student1,student2,student3);
oos.writeObject(arrayList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
从指定文件中读取对象
package com.uwo9.test03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class Test02 {
public static void main(String[] args) {
// 从指定的文件中读取对象
File file = new File("E:/Temp/Test1.txt");
ObjectInputStream ois=null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
// 读取对象
// Student stu = (Student)ois.readObject();
// System.out.println("读取到的数据为:"+stu);
@SuppressWarnings("unchecked")
ArrayList<Student> arrayList = (ArrayList<Student>) ois.readObject();
for (Student student : arrayList) {
System.out.println(student);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
来源:https://blog.csdn.net/huanyinghanlang/article/details/78828347


猜你喜欢
- 这篇山寨一个新版QQ的列表滑动删除,上篇有说到QQ的滑动删除,推测原理就是ListView本身每个item存在一个Button,只不过普通的
- 使用volley进行网络请求:需先将volley包导入androidstudio中File下的Project Structrue,点加号导包
- 最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子。首先,需要在pom文件中依赖以下jar
- public static void main(String args[]) {Map<String, Object> map
- 如果我们在Intellij Idea中开发好程序,需要部署到远程SSH服务器运行,我们可以使用某些SSH软件的rz功能,也可以使用专用的FT
- 最近在鼓捣spring -boot ,真好用,学习到jpa.通过生成Entity 文件,能够快速的生成数据库,并且使用JpaReposito
- 本文介绍了SpringBoot结合SpringSecurity实现图形验证码功能,分享给大家,具体如下:生成图形验证码根据随机数生成图片将随
- 做多媒体项目时,经常会最后来个客户签名并保存之类的,签名保存之前的博客Unity3d截图方法合集有介绍过了,今天闲着把断笔写字的也贴出来吧,
- 微信公众号开发一般是针对企业和组织的,个人一般只能申请订阅号,并且调用的接口有限,下面我们就来简单的描述下接入公众号的步骤:1、首先你需要一
- 以下是测试代码:新建一个classlibrary,包含两个类class1和class2,这两个类中分别有一个方法,都是返回一个字符串,代码如
- 写在前面:上周末抽点时间把自己写的一个简单Socket聊天程序的初始设计和服务端细化设计记录了一下,周二终于等来毕业 * 的软考证书,然后接下
- 一.求两直线交点class Point { double x; do
- Druid动态数据源配置 主要是继承AbstractRoutingDataSource再通过AOP来实现动态数据源切换.下面给大家介绍Dru
- 本文实例为大家分享了C#通过NPOI导入导出数据EXCEL的具体代码,供大家参考,具体内容如下其实从数据库到服务器导入导出有很多方法,但是比
- java中this与super关键字的使用方法这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各
- 绑定多个按钮到同一个事件1.添加代码private void clauseElementClicked(object sender, Eve
- Spring Boot 为 Spring MVC 提供了自动配置,适用于大多数应用程序。官方文档描述:自动配置在 Spring 的默认值之上
- //加载Excel public 
- 题目要求思路一:模拟迭代依次判断每个节点是否合法:左子树判断是否>low,合法就向左下走,不合法往右下;右子树判断是否<high
- 前言哎呀,妈呀,又出异常了!俗话说:“代码虐我千百遍,我待代码如初恋”。小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了