Java I/O 教程(十) ObjectOutputStream和ObjectInputStream
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)
|
例子
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()
|
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的更多相关文章
- Java NIO系列教程(十)DatagramChannel
Java NIO系列教程(十)DatagramChannel 转载自并发编程网 – ifeve.com,本文链接地址: Java NIO系列教程(十) Java NIO DatagramChannel
- 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法
今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...
- Java NIO系列教程(三-十二) Buffer
原文链接 作者:Jakob Jenkov 译者:airu 校对:丁一 Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到 ...
- 201521123061 《Java程序设计》第十二周学习总结
201521123061 <Java程序设计>第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对 ...
- 201521123072《java程序设计》第十二周学习总结
201521123072<java程序设计>第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象 ...
- 201521123038 《Java程序设计》 第十二周学习总结
201521123038 <Java程序设计> 第十二周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student ...
- 201521123122 《java程序设计》第十二周学习总结
## 201521123122 <java程序设计>第十二周实验总结 ## 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将St ...
- 20155306 2016-2017-2 《Java程序设计》第十周学习总结
20155306 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 Java和Android开发学习(网络) 网络概览 计算机网络体系结构的通信协议划分为七层, ...
- ObjectOutputStream和ObjectInputStream
官方解释: ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream.可以使用 ObjectInputStream 读取(重构)对象.通过使用流中的文 ...
- [转] java书籍(给Java程序猿们推荐一些值得一看的好书 + 7本免费的Java电子书和教程 )
7本免费的Java电子书和教程 1. Thinking in Java (Third Edition) 本书的作者是Bruce Eckel,它一直都是Java最畅销的免费电子书.这本书可以帮助你系统的 ...
随机推荐
- 持续集成指南:Gitlab CI/CD 自动部署前端项目
前言 之前陆续写了 Gitlab 的安装使用还有 Gitlab CI/CD 的配置使用,已经把 AspNetCore 的后端项目都做了持续集成了,尝到甜头之后,现在前端的项目也要加入自动化部署,所以经 ...
- [转帖]「开源摘星计划」Prometheus监控Harbor(二进制版)
推荐 原创 键客李大白2022-08-08 11:35:07博主文章分类:Harbor进阶实战(企业实战)著作权 文章标签云原生运维Harbor文章分类kubernetes云计算私藏项目实操分享阅读数 ...
- Linux无头模式使用mat分析dump的方法
摘要 mat可以很好的进行jvm的内存dump的分析. 但是大部分服务器是没有GUI界面的. 而且就算是有GUI界面也很难直接使用. 但是随着jvm堆区越来越大. WindowsPC机器已经很难进行分 ...
- MYSQL 日志参数与性能的关系
1. 先看一下mysql技术内幕 innodb存储引擎的一个结果 以及各个参数的含义
- css3中的圆角border-radius
css3的属性应该加上浏览器前缀 不同的浏览器内核不同,因此css的前缀不同. 常见的几种浏览器内核 火狐浏览器 Geoko内核 前缀是 -mox- 谷歌浏览器, Webkit内核 前缀是 -wekb ...
- Gin 项目引入热加载
目录 一.什么是热加载 二.Air 2.1 介绍 2.2 特性 特性: 2.3 相关文档 2.4 安装 推荐使用 install.sh 使用 go install 2.5 配置环境变量 2.6 使用 ...
- Typora 1.6.7永久激活
介绍Typora介绍 具体看上面的我就不多介绍了 接下来我们开始教程 需要的文件 Typora安装包 破解补丁包 安装包下载 破解补丁下载 接下来我们全部下载后获得一个安装包一个补丁 安装包直接安装就 ...
- 开启中文智能之旅:探秘超乎想象的 Llama2-Chinese 大模型世界
"开启中文智能之旅:探秘超乎想象的 Llama2-Chinese 大模型世界" 1.国内Llama2最新下载地址 本仓库中的代码示例主要是基于Hugging Face版本参数进行调 ...
- 语义检索系统:基于无监督预训练语义索引召回:SimCSE、Diffcse
基于无监督预训练语义索引召回:SimCSE.Diffcse 语义索引(可通俗理解为向量索引)技术是搜索引擎.推荐系统.广告系统在召回阶段的核心技术之一.语义索引模型的目标是:给定输入文本,模型可以从海 ...
- paddle DeBug 三步定位PARL飞桨报错原因,快速解决程序问题
相关文章: [一]-环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简介 [五]-Sarsa&Qlea ...