java 对象输入输出流读写文件的操作实例
作者:nello 发布时间:2021-06-13 15:24:30
标签:java,流
java 对象输入输出流读写文件的操作实例
java 支持对对象的读写操作,所操作的对象必须实现Serializable接口。
实例代码:
package vo;
import java.io.Serializable;
public class Animal implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Integer weight;
private String color;
private String type;
private Integer age;
private Integer lifetime;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getLifetime() {
return lifetime;
}
public void setLifetime(Integer lifetime) {
this.lifetime = lifetime;
}
public Animal(String name, Integer weight, String color, String type, Integer age, Integer lifetime) {
super();
this.name = name;
this.weight = weight;
this.color = color;
this.type = type;
this.age = age;
this.lifetime = lifetime;
}
@Override
public String toString() {
return "Animal [name=" + name + ", weight=" + weight + ", color=" + color + ", type=" + type + ", age=" + age + ", lifetime=" + lifetime + "]";
}
}
package objectstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import vo.Animal;
public class TestObjectStream {
public static void main(String[] args) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:/oos.dat")));
Animal a1 = new Animal("tiger", 120, "red", "cat", 12, 20);
Animal a2 = new Animal("eagle", 10, "gold", "bird", 6, 10);
oos.writeObject(a1);
oos.writeObject(a2);
oos.flush();
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/oos.dat"));
Animal ra1 = (Animal) ois.readObject();
System.out.println(ra1.toString());
Animal ra2 = (Animal) ois.readObject();
System.out.println(ra2.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果:
Animal [name=tiger, weight=120, color=red, type=cat, age=12, lifetime=20]
Animal [name=eagle, weight=10, color=gold, type=bird, age=6, lifetime=10]
如有疑问请留言或者到本站社区交流讨论,本站关于java开发的文章还有很多,希望大家搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


猜你喜欢
- 本文实例讲述了Java实现批量导入excel表格数据到数据库中的方法。分享给大家供大家参考,具体如下:1、创建导入抽象类package co
- 本文实例为大家分享了C语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下IDE用的是 VS2019先看效果 代码全览game.
- 本文实例讲述了Android基本游戏循环。分享给大家供大家参考。具体如下:// desired fpsprivate final stati
- 在对类访问使用时,常用到的有访问类的成员、方法。实例化在对类进行访问时,需要将类进行实例化。并产生一个对象。可以使用关键字new来实现。由于
- 本文实例讲述了Java实现的zip工具类。分享给大家供大家参考,具体如下:实现把zip解压到指定路径,把文件夹压缩到zip,把文件列表压缩为
- * 可以说是Android开发中最常用的东西之一。我们通过 * 可以监听对象的各种变化事件,并进行一些需要的处理,相当有用,而且使用起来也
- 本文实例讲述了C#设计模式之Builder生成器模式解决带老婆配置电脑问题。分享给大家供大家参考,具体如下:一、理论定义生成器模式 又叫:建
- 1.问题在MyBatisPlus中经常会用到如下所示的代码来构造查询条件:QueryWrapper<User> queryWra
- 刚开始我以为熔断和降级是一体的,以为他们必须配合使用; 只不过名字不一样而已,但是当我经过思考过后,发现他们其实不是一个东西;降级什么是服务
- 本文实例为大家分享了java查找图中两点之间所有路径的具体代码,基于邻接表,供大家参考,具体内容如下图类:package graph1;im
- 常用命令:打包:mvn package编译:mvn compile清空:mvn clean(清除编译后目录,默认是target目录)运行测试
- 目录1、需求2、问题2、获取1)导入依赖为了获取客户端类型、操作系统类型、ip、port2)封装获取body字符串的工具类3) * 类4)继
- cc3利用链如下:TrAXFilter(Templates templates) TemplatesImpl-&a
- 说到事件机制,可能脑海中最先浮现的就是日常使用的各种 listener,listener去监听事件源,如果被监听的事件有变化就会通知list
- 平时我们在开发过程中,代码出现bug时为了更好的在服务器日志中寻找问题根源,会在接口的首尾打印日志,看下参数和返回值是否有问题。但是手动的l
- 最近做项目中遇到ToolBar因为不同的界面toobar不同为了描述统一的风格。相信大家也非常清楚,大多数ToolBar包括以下几个方面左标
- 1. 引入jar包pom.xml文件<?xml version="1.0" encoding="UTF-
- 上篇文章给大家介绍了浅析C# 中的类型系统(值类型和引用类型),接下来通过本文给大家介绍下c# 泛型类型,说下C#中的泛型,熟练地使用泛型能
- 使用commons的jexl可实现将字符串变成可执行代码的功能,我写了一个类来封装这个功能:import java.util.Map;imp
- using Microsoft.Win32 ; 1.读取指定名称的注册表的值 &nbs