java io系列05之 ObjectInputStream 和 ObjectOutputStream
本章,我们学习ObjectInputStream 和 ObjectOutputStream
ObjectInputStream 和 ObjectOutputStream 介绍
ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持。
创建“文件输出流”对应的ObjectOutputStream对象,该ObjectOutputStream对象能提供对“基本数据或对象”的持久存储;当我们需要读取这些存储的“基本数据或对象”时,可以创建“文件输入流”对应的ObjectInputStream,进而读取出这些“基本数据或对象”。
注意: 只有支持 java.io.Serializable 或 java.io.Externalizable 接口的对象才能被ObjectInputStream/ObjectOutputStream所操作!
转载请注明出处: http://www.cnblogs.com/skywang12345/p/io_05.html
ObjectOutputStream 函数列表
// 构造函数
ObjectOutputStream(OutputStream output)
// public函数
void close()
void defaultWriteObject()
void flush()
ObjectOutputStream.PutField putFields()
void reset()
void useProtocolVersion(int version)
void write(int value)
void write(byte[] buffer, int offset, int length)
void writeBoolean(boolean value)
void writeByte(int value)
void writeBytes(String value)
void writeChar(int value)
void writeChars(String value)
void writeDouble(double value)
void writeFields()
void writeFloat(float value)
void writeInt(int value)
void writeLong(long value)
final void writeObject(Object object)
void writeShort(int value)
void writeUTF(String value)
void writeUnshared(Object object)
ObjectInputStream 函数列表
// 构造函数
ObjectInputStream(InputStream input) int available()
void close()
void defaultReadObject()
int read(byte[] buffer, int offset, int length)
int read()
boolean readBoolean()
byte readByte()
char readChar()
double readDouble()
ObjectInputStream.GetField readFields()
float readFloat()
void readFully(byte[] dst)
void readFully(byte[] dst, int offset, int byteCount)
int readInt()
String readLine()
long readLong()
final Object readObject()
short readShort()
String readUTF()
Object readUnshared()
int readUnsignedByte()
int readUnsignedShort()
synchronized void registerValidation(ObjectInputValidation object, int priority)
int skipBytes(int length)
演示程序
源码如下(ObjectStreamTest.java):
/**
* ObjectInputStream 和 ObjectOutputStream 测试程序
*
* 注意:通过ObjectInputStream, ObjectOutputStream操作的对象,必须是实现了Serializable或Externalizable序列化接口的类的实例。
*
* @author skywang
*/ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator; public class ObjectStreamTest {
private static final String TMP_FILE = "box.tmp"; public static void main(String[] args) {
testWrite();
testRead();
} /**
* ObjectOutputStream 测试函数
*/
private static void testWrite() {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(TMP_FILE));
out.writeBoolean(true);
out.writeByte((byte)65);
out.writeChar('a');
out.writeInt(20131015);
out.writeFloat(3.14F);
out.writeDouble(1.414D);
// 写入HashMap对象
HashMap map = new HashMap();
map.put("one", "red");
map.put("two", "green");
map.put("three", "blue");
out.writeObject(map);
// 写入自定义的Box对象,Box实现了Serializable接口
Box box = new Box("desk", 80, 48);
out.writeObject(box); out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} /**
* ObjectInputStream 测试函数
*/
private static void testRead() {
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(TMP_FILE));
System.out.printf("boolean:%b\n" , in.readBoolean());
System.out.printf("byte:%d\n" , (in.readByte()&0xff));
System.out.printf("char:%c\n" , in.readChar());
System.out.printf("int:%d\n" , in.readInt());
System.out.printf("float:%f\n" , in.readFloat());
System.out.printf("double:%f\n" , in.readDouble());
// 读取HashMap对象
HashMap map = (HashMap) in.readObject();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
System.out.printf("%-6s -- %s\n" , entry.getKey(), entry.getValue());
}
// 读取Box对象,Box实现了Serializable接口
Box box = (Box) in.readObject();
System.out.println("box: " + box); in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} class Box implements Serializable {
private int width;
private int height;
private String name; public Box(String name, int width, int height) {
this.name = name;
this.width = width;
this.height = height;
} @Override
public String toString() {
return "["+name+": ("+width+", "+height+") ]";
}
}
运行结果:
boolean:true
byte:65
char:a
int:20131015
float:3.140000
double:1.414000
two -- green
one -- red
three -- blue
box: [desk: (80, 48) ]
更多内容
3. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)
4. java io系列03之 ByteArrayOutputStream的简介,源码分析和示例(包括OutputStream
5. java io系列04之 管道(PipedOutputStream和PipedInputStream)的简介,源码分析和示例
java io系列05之 ObjectInputStream 和 ObjectOutputStream的更多相关文章
- Java IO(六) ObjectInputStream 和 ObjectOutputStream
Java IO(六) ObjectInputStream 和 ObjectOutputStream 一.介绍 对于对象数据的处理,Java IO 提供了 ObjectInputStream 和 Obj ...
- java io系列01之 "目录"
java io 系列目录如下: 01. java io系列01之 "目录" 02. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括 ...
- java io系列06之 序列化总结(Serializable 和 Externalizable)
本章,我们对序列化进行深入的学习和探讨.学习内容,包括序列化的作用.用途.用法,以及对实现序列化的2种方式Serializable和Externalizable的深入研究. 转载请注明出处:http: ...
- java io系列08之 File总结
本文对File的API和常用方法进行介绍. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_08.html File 介绍 File 是“文件”和“目 ...
- java io系列12之 BufferedInputStream(缓冲输入流)的认知、源码和示例
本章内容包括3个部分:BufferedInputStream介绍,BufferedInputStream源码,以及BufferedInputStream使用示例. 转载请注明出处:http://www ...
- java io系列13之 BufferedOutputStream(缓冲输出流)的认知、源码和示例
本章内容包括3个部分:BufferedOutputStream介绍,BufferedOutputStream源码,以及BufferedOutputStream使用示例. 转载请注明出处:http:// ...
- java io系列
java io系列01之 "目录" java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream) java io系列03之 ...
- Java IO系列之一:IO
1. 概述 Java IO一般包含两个部分: 1.java.io包中堵塞型IO: 2.java.nio包中的非堵塞型IO,通常称为New IO. java.io包下,分为四大块近80个类: 1.基于字 ...
- Java 集合系列 05 Vector详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
随机推荐
- kubernetes 基本命令
查询命令: kubectl get pods -n kube-system kubectl get ClusterRole -n kube-system kubectl get ClusterRole ...
- git 出现错误时
Your local changes to the following files would be overwritten by merge: 解决办法 如果希望保留生产服务器上所做的改动,仅仅并入 ...
- 第二十一天,pickle json xml shelve configparser模块
今日内容 1.pcikle 专用于python语言的序列化 2.json 是一种跨平台的数据格式 也属于序列化的一种方式 3.xml 可拓展标记语言 一种编写文档的语法 也支持跨平台 比较json而言 ...
- Gym100496H-House of Representatives-树
树上每个元素有一个p,元素之间有距离d,计算一个元素u,使得sigma(d(i,u)*pi)最小. 两次dfs,第一次计算本节点以下的sigma(),第二次利用sump求解出ans. #include ...
- windows 环境下通过运行快速启动程序
在windows环境下,我们可以使用一些系统内置的快捷键来快速启动我们想要的应用程序,我这里举例几个我经常使用的,比如: 快捷键 功能说明 services.msc 查看系统服务 gpedit.ms ...
- Codeforces340 E. Iahub and Permutations
Codeforces题号:#340E 出处: Codeforces 主要算法:思维+DP 难度:4.8 题意: 有一个长度为$n$的排列(即各元素互不相同),其中有一些为-1.现要求将数填到这些-1上 ...
- Mysql 函数大全- 5.6 中文解释函数参考
mysql 函数大全 5.6 函数参考 5.6函数参考 (只翻译部分,细节查看相关英文版) 12.1功能和操作员参考 12.2表达式评估中的类型转换 12.3运营商 12.4控制流功能 12.5 ...
- 缺省源和 Vim 配置
缺省源 #include <bits/stdc++.h> #define x first #define y second #define pb push_back #define mp ...
- iptable四表五链
链(内置): PREROUTING:对数据包作路由选择前应用此链中的规则: INPUT:进来的数据包应用此规则链中的策略: FORWARD:转发数据包时应用此规则链中的策略: OUTPUT:外出的数据 ...
- luogu1608 路径统计 (spfa)
题意:给一个有向图(无零边),要求找出最短路的数量(重边只计算一次) 做spfa的时候,记一个cnt对于u-w->v如果dis[u]+w=dis[v],cnt[v]+=cnt[u] 如果dis[ ...