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 ...
随机推荐
- python学习之装饰器-
python的装饰器 2018-02-26 在了解python的装饰器之前我们得了解python的高阶函数 python的高阶函数我们能返回一个函数名并且能将函数名作为参数传递 def outer() ...
- IE6浏览器有哪些常见的bug,缺陷或者与标准不一致的地方,如何解决
IE6不支持min-height,解决办法使用css hack: .target { min-height: 100px; height: auto !important; height: 100px ...
- js登录,回车登录
$(document).ready(function(){ $("#loginBtn").click(doLoginEvent); loadCookies(); //回车登录 do ...
- Safari 3D transform变换z-index层级渲染异常
(猛戳来源:http://www.zhangxinxu.com/wordpress/?p=5569)
- mybatis choose标签的使用
MyBatis 提供了 choose 元素.if标签是与(and)的关系,而 choose 是或(or)的关系. choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立 ...
- 重新设置Eclipse的workspace路径
有3中方法可以更改workspace的路径设置: 1. 启动Eclipse/MyEclipse后, 打开"Window -> Preferences -> General -&g ...
- 设置元素text-overflow: ellipsis后引起的文本对齐问题
.ellipsis { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } 给元素设置了这个属性之后,该行内元素和旁边的 ...
- html学习中
Html常用标签一 <body style="background-color:red;"> Body 标签 Style 属性 background-color:red ...
- 转:window与linux互相拷贝文件
window与linux互相拷贝文件 借助 PSCP 命令可以实现文件的互拷: 1.下载pscp.exe 文件 (我的资源文件中有) 2.如果想在所有目录可以执行,请更改环境变量. w ...
- ES6(Decorator(修饰器))
Decorator(修饰器) 1.基本概念 函数用来修改 类 的行为 1.Decorator 是一个函数 2.通过Decorator(修饰器)能修改 类 的行为(扩展 类 的功能)3.Decorato ...