本章,我们学习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) ]

更多内容

1. java 集合系列目录(Category)

2. java io系列01之 IO框架

3. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)

4. java io系列03之 ByteArrayOutputStream的简介,源码分析和示例(包括OutputStream

5. java io系列04之 管道(PipedOutputStream和PipedInputStream)的简介,源码分析和示例

最新文章

  1. javascrit原生实现jquery的append()函数
  2. 跨域资源共享(CORS)在ASP.NET Web API中是如何实现的?
  3. java环境基础步骤 maven
  4. 面向对象tab栏例子分析
  5. C#开发BHO插件UrlTrack
  6. Unity 切割导出精灵
  7. (详细)华为Mate7 MT7-TL00的usb调试模式在哪里开启的步骤
  8. time-时间模块
  9. UWB DWM1000 跟随小车原理---一张图演示
  10. pip install
  11. 程序员修神之路--🤠分布式高并发下Actor模型如此优秀🤠
  12. uva-108-贪心
  13. Python import其他文件夹的文件
  14. Performance testing test scenarios
  15. Java 多线程之 Thread 类 和 Runnable 接口初步使用
  16. 002.SSH日常命令
  17. git push origin master:master
  18. Mongodb之使用rpm包安装配置启动
  19. Android开发--第一个活动
  20. 网站Web性能测试:ApacheBench,Webbench,http_load使用教程

热门文章

  1. UESTC1013-我的魔法栈-模拟/排列组合
  2. Codeforces510 C. Fox And Names
  3. MT【303】估计
  4. [loj6388] 「THUPC2018」赛艇 / Citing
  5. Hdoj 1159.Common Subsequence 题解
  6. shell getopts用法
  7. docker-compose.yml(2)
  8. Rocket.Chat 开源IM系统部署
  9. C语言中类型转换#大写字母转小写字母和小写字母转大写字母案例。
  10. BZOJ 1143: [CTSC2008]祭祀river(最大独立集)