ObjectOutputStream

ObjectOutputStream用于往输出流中写入原始类型和Java对象。

类定义

public class ObjectOutputStream
extends OutputStream
implements ObjectOutput, ObjectStreamConstants

构造函数

protected ObjectOutputStream()

Provide a way for subclasses that are completely reimplementing ObjectOutputStream to not have to allocate private data just used by this implementation of ObjectOutputStream.
  ObjectOutputStream(OutputStream out)

Creates an ObjectOutputStream that writes to the specified OutputStream.

方法

protected void annotateClass(Class<?> cl)

Subclasses may implement this method to allow class data to be stored in the stream.
protected void annotateProxyClass(Class<?> cl)

Subclasses may implement this method to store custom data in the stream along with descriptors for dynamic proxy classes.
void close()

Closes the stream.
void defaultWriteObject()

Write the non-static and non-transient fields of the current class to this stream.
protected void drain()

Drain any buffered data in ObjectOutputStream.
protected boolean enableReplaceObject(boolean enable)

Enable the stream to do replacement of objects in the stream.
void flush()

Flushes the stream.
ObjectOutputStream.PutField putFields()

Retrieve the object used to buffer persistent fields to be written to the stream.
protected Object replaceObject(Object obj)

This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization.
void reset()

Reset will disregard the state of any objects already written to the stream.
void useProtocolVersion(int version)

Specify stream protocol version to use when writing the stream.
void write(byte[] buf)

Writes an array of bytes.
void write(byte[] buf, int off, int len)

Writes a sub array of bytes.
void write(int val)

Writes a byte.
void writeBoolean(boolean val)

Writes a boolean.
void writeByte(int val)

Writes an 8 bit byte.
void writeBytes(String str)

Writes a String as a sequence of bytes.
void writeChar(int val)

Writes a 16 bit char.
void writeChars(String str)

Writes a String as a sequence of chars.
protected void writeClassDescriptor(ObjectStreamClass desc)

Write the specified class descriptor to the ObjectOutputStream.
void writeDouble(double val)

Writes a 64 bit double.
void writeFields()

Write the buffered fields to the stream.
void writeFloat(float val)

Writes a 32 bit float.
void writeInt(int val)

Writes a 32 bit int.
void writeLong(long val)

Writes a 64 bit long.
void writeObject(Object obj)

Write the specified object to the ObjectOutputStream.
protected void writeObjectOverride(Object obj)

Method used by subclasses to override the default writeObject method.
void writeShort(int val)

Writes a 16 bit short.
protected void writeStreamHeader()

The writeStreamHeader method is provided so subclasses can append or prepend their own header to the stream.
void writeUnshared(Object obj)

Writes an "unshared" object to the ObjectOutputStream.
void writeUTF(String str)

Primitive data write of this String in
 
modified UTF-8
 format.

例子

FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345);
oos.writeObject("Today");
oos.writeObject(new Date()); oos.close();

ObjectInputStream

ObjectInputStream用于从输入流读取Java对象。

类定义

public class ObjectInputStream
extends InputStream
implements ObjectInput, ObjectStreamConstants

构造函数

protected ObjectInputStream()

Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream.
  ObjectInputStream(InputStream in)

Creates an ObjectInputStream that reads from the specified InputStream.

方法

int available()

Returns the number of bytes that can be read without blocking.
void close()

Closes the input stream.
void defaultReadObject()

Read the non-static and non-transient fields of the current class from this stream.
protected boolean enableResolveObject(boolean enable)

Enable the stream to allow objects read from the stream to be replaced.
int read()

Reads a byte of data.
int read(byte[] buf, int off, int len)

Reads into an array of bytes.
boolean readBoolean()

Reads in a boolean.
byte readByte()

Reads an 8 bit byte.
char readChar()

Reads a 16 bit char.
protected ObjectStreamClass readClassDescriptor()

Read a class descriptor from the serialization stream.
double readDouble()

Reads a 64 bit double.
ObjectInputStream.GetField readFields()

Reads the persistent fields from the stream and makes them available by name.
float readFloat()

Reads a 32 bit float.
void readFully(byte[] buf)

Reads bytes, blocking until all bytes are read.
void readFully(byte[] buf, int off, int len)

Reads bytes, blocking until all bytes are read.
int readInt()

Reads a 32 bit int.
String readLine()

Deprecated.

This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives.

long readLong()

Reads a 64 bit long.
Object readObject()

Read an object from the ObjectInputStream.
protected Object readObjectOverride()

This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor.
short readShort()

Reads a 16 bit short.
protected void readStreamHeader()

The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers.
Object readUnshared()

Reads an "unshared" object from the ObjectInputStream.
int readUnsignedByte()

Reads an unsigned 8 bit byte.
int readUnsignedShort()

Reads an unsigned 16 bit short.
String readUTF()

Reads a String in
 
modified UTF-8
 format.
void registerValidation(ObjectInputValidation obj, int prio)

Register an object to be validated before the graph is returned.
protected Class<?> resolveClass(ObjectStreamClass desc)

Load the local class equivalent of the specified stream class description.
protected Object resolveObject(Object obj)

This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization.
protected Class<?> resolveProxyClass(String[] interfaces)

Returns a proxy class that implements the interfaces named in a proxy class descriptor; subclasses may implement this method to read custom data from the stream along with the descriptors for dynamic proxy classes, allowing them to use an alternate loading mechanism for the interfaces and the proxy class.
int skipBytes(int len)

Skips bytes.

例子

FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis); int i = ois.readInt();
String today = (String) ois.readObject();
Date date = (Date) ois.readObject(); ois.close();

关于读取Java对象的例子,请参考文章:

Java序列化(Serializable)与反序列化详解

Java I/O 教程(十) ObjectOutputStream和ObjectInputStream的更多相关文章

  1. Java NIO系列教程(十)DatagramChannel

    Java NIO系列教程(十)DatagramChannel 转载自并发编程网 – ifeve.com,本文链接地址: Java NIO系列教程(十) Java NIO DatagramChannel

  2. 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法

    今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...

  3. Java NIO系列教程(三-十二) Buffer

    原文链接     作者:Jakob Jenkov     译者:airu     校对:丁一 Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到 ...

  4. 201521123061 《Java程序设计》第十二周学习总结

    201521123061 <Java程序设计>第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对 ...

  5. 201521123072《java程序设计》第十二周学习总结

    201521123072<java程序设计>第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象 ...

  6. 201521123038 《Java程序设计》 第十二周学习总结

    201521123038 <Java程序设计> 第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student ...

  7. 201521123122 《java程序设计》第十二周学习总结

    ## 201521123122 <java程序设计>第十二周实验总结 ## 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将St ...

  8. 20155306 2016-2017-2 《Java程序设计》第十周学习总结

    20155306 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 Java和Android开发学习(网络) 网络概览 计算机网络体系结构的通信协议划分为七层, ...

  9. ObjectOutputStream和ObjectInputStream

    官方解释: ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream.可以使用 ObjectInputStream 读取(重构)对象.通过使用流中的文 ...

  10. [转] java书籍(给Java程序猿们推荐一些值得一看的好书 + 7本免费的Java电子书和教程 )

    7本免费的Java电子书和教程 1. Thinking in Java (Third Edition) 本书的作者是Bruce Eckel,它一直都是Java最畅销的免费电子书.这本书可以帮助你系统的 ...

随机推荐

  1. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.12.18)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  2. 阿里云龙蜥8.6部署SQLSERVER2022的过程

    阿里云龙蜥8.6部署SQLSERVER2022的过程 背景 之前总结过, 但是发现当时是preview版本. 这里想升级一下, 并且顺便抄一下他的部分说明 下载 wget https://packag ...

  3. 阿里云IPV6 创建虚拟机的过程

    阿里云IPV6 创建虚拟机的过程 背景 IPV6 已经越来越广泛的应用. 想在外网开通一下IPV6,发现还有一些坑. 这里总结一下. 备忘. 开通方式 1. 登录阿里云的控制台, 打开云服务器ECS的 ...

  4. [转帖]Linux中查看各文件夹大小命令du -h --max-depth=1

    https://www.cnblogs.com/the-tops/p/8798678.html 最近排查服务器异常的时候,常会遇到磁盘慢的情况,这个时候,查找那个文件夹占用的内存的时候常用到这个命令: ...

  5. [转帖]Linux之pure-ftpd安装和使用

    一.pure-ftpd简介   PureFTPd是一款专注于程序健壮和软件安全的免费FTP服务器软件(基于BSD License),以安全和配置简单为设计目标,支持虚拟主机,IPV6,PAM等功能.. ...

  6. Linux 内核参数

    /proc/sys/net/ipv4: ip_local_port_range:定义了TCP或UDP对目标发起连接所选择的本地端口范围(除ip_local_reserved_ports之外),其定义受 ...

  7. 我对vue3的理解

    我对 reactive源码的理解 reactive 只能够代理对象 首先它判断传递过来的值是否是对象,如果是才会进行代理.变成响应式的. Proxy 并没有重写对象的属性,只做代理,在取值的时候回调用 ...

  8. vue3中retive的错误用法导致数据不跟新

    retive的错误用法 <template> <div> 司藤的信息==>{{ objInfo }} <button @click="handerHttp ...

  9. mysql系列基础篇01---通用的语法及分类

    通用语法及分类 DDL: 数据定义语言,用来定义数据库对象(数据库.表.字段) DML: 数据操作语言,用来对数据库表中的数据进行增删改 DQL: 数据查询语言,用来查询数据库中表的记录 DCL: 数 ...

  10. Linux基础命令 [补档-2023-06-28]

    Linux基础命令 1-1.命令的基本格式 ​ Linux系统命令的通用格式为: ​ command [-options] [parameter] ​ 其中 ​ -command 命令本身 ​ -op ...