Java-IO之对象输入流输出流(ObjectInputStream和ObjectOutputStream)
ObjectInputStream和ObjectOutputStream的作用是对基本数据和对象进行序列化操作支持。创建文件输出流对应的ObjectOutputStream对象,该ObjectOutputStream对象能提供对基本数据或对象的持久存储,当我们需要读取这些存储的基本数据或对象时,可以创建文件输入流对应的ObjectInputStream,进而读取这些基本数据或对象。
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)
示例代码:
clas 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+") ]";
}
}
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();
}
}
}
Java-IO之对象输入流输出流(ObjectInputStream和ObjectOutputStream)的更多相关文章
- (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象的序列化与反序列化)
如题 所有关联的类需要继承Serializable 接口 文件为空,直接反序列化为发生错误; 毕竟对象为null , 序列化到文件里不是空空的! 以下笔记的原文连接: https://www.cnbl ...
- Java IO(Properties/对象序列化/打印流/commons-io)
Java IO(Properties/对象序列化/打印流/commons-io) Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载. ...
- Java——IO流 对象的序列化和反序列化流ObjectOutputStream和ObjectInputStream
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- Java IO流对象、多线程
Input(读) Output(写)操作 File类 import java.io.File; 将操作系统中的文件.目录(文件夹).路径.封装成File对象 提供方法,操作系统中的内容.File与系统 ...
- 输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- Java—IO流 对象的序列化和反序列化
序列化的基本操作 1.对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化. 2.序列化流(ObjectOutputStream),writeObject 方法用于将对象写入输出流中 ...
- Java Io 流(输入输出流)
IO流,也就是输入和输出流,可分为字节流和字符流. 1. 字节流 (1). InputStream 输入流,用于读取文件 输入流常用API: inputStream.read() 读取一个字节 in ...
随机推荐
- jvm(三):对象
关于对象,我们需要面对的问题主要有对象的创建,对象在内存中的布局,对象的结构,对象的访问定位. 对象的创建 对象的创建过程如下图所示: 其主要步骤有:给对象分配内存,初始化对象,执行构造方法. 在对象 ...
- Map,HashMap,TreeMap
一.HashMap,TreeMap差别 1.两种常规Map性能 HashMap:适用于在Map中插入.删除和定位元素. Treemap:适用于按自然顺序或自定义顺序遍历键(key). 2.总结 Has ...
- HTTP 协议详解(超级经典)-转
什么是HTTP协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端 ...
- Bootstrap3 表单-被支持的控件:文本域
支持多行文本的表单控件.可根据需要改变 rows 属性. <textarea class="form-control" rows="3"></ ...
- audio session config
#pragma mark - #pragma mark - audio session config - (void)setAudioSessionConfig { NSError *error; A ...
- Dynamics CRM2016 使用web api来创建注释时的注意事项
在使用wei api 创建注释的时候,有个字段需要注意下,就是下面图中的objectid字段,虽然它是个查找字段,但不能像普通的查找字段property@odata.bind来赋值 上代码,注意看倒数 ...
- 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...
- 2016年年终CSDN博客总结
2015年12月1日,结束了4个月的尚观嵌入式培训生涯,经过了几轮重重面试,最终来到了伟易达集团.经过了长达3个月的试用期,正式成为了伟易达集团的助理工程师. 回顾一年来的学习,工作,生活.各种酸甜苦 ...
- springMVC源码分析--AbstractControllerUrlHandlerMapping(六)
上一篇博客springMVC源码分析--AbstractDetectingUrlHandlerMapping(五)中我们介绍了AbstractDetectingUrlHandlerMapping,其定 ...
- 使用Xpath定位元素(和元素定位相关的Xpath语法)
本文主要讲述Xpath语法中,和元素定位相关的语法 第一种方法:通过绝对路径做定位(相信大家不会使用这种方式) By.xpath("html/body/div/form/input" ...