ObjectInputStream和ObjectOutputStream的作用是对基本数据和对象进行序列化操作支持。创建文件输出流对应的ObjectOutputStream对象,该ObjectOutputStream对象能提供对基本数据或对象的持久存储,当我们需要读取这些存储的基本数据或对象时,可以创建文件输入流对应的ObjectInputStream,进而读取这些基本数据或对象。

ObjectOutputStream类主要函数:

  1. // 构造函数
  2. ObjectOutputStream(OutputStream output)
  3. // public函数
  4. void close()
  5. void defaultWriteObject()
  6. void flush()
  7. ObjectOutputStream.PutField putFields()
  8. void reset()
  9. void useProtocolVersion(int version)
  10. void write(int value)
  11. void write(byte[] buffer, int offset, int length)
  12. void writeBoolean(boolean value)
  13. void writeByte(int value)
  14. void writeBytes(String value)
  15. void writeChar(int value)
  16. void writeChars(String value)
  17. void writeDouble(double value)
  18. void writeFields()
  19. void writeFloat(float value)
  20. void writeInt(int value)
  21. void writeLong(long value)
  22. final void writeObject(Object object)
  23. void writeShort(int value)
  24. void writeUTF(String value)
  25. void writeUnshared(Object object)

ObjectInputStream类主要函数:

  1. // 构造函数
  2. ObjectInputStream(InputStream input)
  3. int available()
  4. void close()
  5. void defaultReadObject()
  6. int read(byte[] buffer, int offset, int length)
  7. int read()
  8. boolean readBoolean()
  9. byte readByte()
  10. char readChar()
  11. double readDouble()
  12. ObjectInputStream.GetField readFields()
  13. float readFloat()
  14. void readFully(byte[] dst)
  15. void readFully(byte[] dst, int offset, int byteCount)
  16. int readInt()
  17. String readLine()
  18. long readLong()
  19. final Object readObject()
  20. short readShort()
  21. String readUTF()
  22. Object readUnshared()
  23. int readUnsignedByte()
  24. int readUnsignedShort()
  25. synchronized void registerValidation(ObjectInputValidation object, int priority)
  26. int skipBytes(int length)

示例代码:

  1. clas Box implements Serializable {
  2. private int width;
  3. private int height;
  4. private String name;
  5. public Box(String name, int width, int height) {
  6. this.name = name;
  7. this.width = width;
  8. this.height = height;
  9. }
  10. @Override
  11. public String toString() {
  12. return "["+name+": ("+width+", "+height+") ]";
  13. }
  14. }
  1. public class ObjectStreamTest {
  2. private static final String TMP_FILE = "box.tmp";
  3.  
  4. public static void main(String[] args) {
  5. testWrite();
  6. testRead();
  7. }
  8.  
  9. /**
  10. * ObjectOutputStream 测试函数
  11. */
  12. private static void testWrite() {
  13. try {
  14. ObjectOutputStream out = new ObjectOutputStream(
  15. new FileOutputStream(TMP_FILE));
  16. out.writeBoolean(true);
  17. out.writeByte((byte)65);
  18. out.writeChar('a');
  19. out.writeInt(20131015);
  20. out.writeFloat(3.14F);
  21. out.writeDouble(1.414D);
  22. // 写入HashMap对象
  23. HashMap map = new HashMap();
  24. map.put("one", "red");
  25. map.put("two", "green");
  26. map.put("three", "blue");
  27. out.writeObject(map);
  28. // 写入自定义的Box对象,Box实现了Serializable接口
  29. Box box = new Box("desk", 80, 48);
  30. out.writeObject(box);
  31.  
  32. out.close();
  33. } catch (Exception ex) {
  34. ex.printStackTrace();
  35. }
  36. }
  37.  
  38. /**
  39. * ObjectInputStream 测试函数
  40. */
  41. private static void testRead() {
  42. try {
  43. ObjectInputStream in = new ObjectInputStream(new FileInputStream(TMP_FILE));
  44. System.out.printf("boolean:%b\n" , in.readBoolean());
  45. System.out.printf("byte:%d\n" , (in.readByte()&0xff));
  46. System.out.printf("char:%c\n" , in.readChar());
  47. System.out.printf("int:%d\n" , in.readInt());
  48. System.out.printf("float:%f\n" , in.readFloat());
  49. System.out.printf("double:%f\n" , in.readDouble());
  50. // 读取HashMap对象
  51. HashMap map = (HashMap) in.readObject();
  52. Iterator iter = map.entrySet().iterator();
  53. while (iter.hasNext()) {
  54. Map.Entry entry = (Map.Entry)iter.next();
  55. System.out.printf("%-6s -- %s\n" , entry.getKey(), entry.getValue());
  56. }
  57. // 读取Box对象,Box实现了Serializable接口
  58. Box box = (Box) in.readObject();
  59. System.out.println("box: " + box);
  60.  
  61. in.close();
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
运行结果:
boolean:true
byte:65
char:a
int:20131015
float:3.140000
double:1.414000
three  -- blue
two    -- green
one    -- red
box: [desk: (80, 48) ]

Java-IO之对象输入流输出流(ObjectInputStream和ObjectOutputStream)的更多相关文章

  1. (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  2. 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  3. 对象输入输出流ObjectInputStream、ObjectOutputStream(对象的序列化与反序列化)

    如题 所有关联的类需要继承Serializable 接口 文件为空,直接反序列化为发生错误; 毕竟对象为null , 序列化到文件里不是空空的! 以下笔记的原文连接: https://www.cnbl ...

  4. Java IO(Properties/对象序列化/打印流/commons-io)

    Java IO(Properties/对象序列化/打印流/commons-io) Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载. ...

  5. Java——IO流 对象的序列化和反序列化流ObjectOutputStream和ObjectInputStream

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  6. Java IO流对象、多线程

    Input(读) Output(写)操作 File类 import java.io.File; 将操作系统中的文件.目录(文件夹).路径.封装成File对象 提供方法,操作系统中的内容.File与系统 ...

  7. 输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  8. Java—IO流 对象的序列化和反序列化

    序列化的基本操作 1.对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化. 2.序列化流(ObjectOutputStream),writeObject 方法用于将对象写入输出流中 ...

  9. Java Io 流(输入输出流)

    IO流,也就是输入和输出流,可分为字节流和字符流. 1. 字节流 (1). InputStream 输入流,用于读取文件 输入流常用API: inputStream.read()  读取一个字节 in ...

随机推荐

  1. python学习之装饰器-

    python的装饰器 2018-02-26 在了解python的装饰器之前我们得了解python的高阶函数 python的高阶函数我们能返回一个函数名并且能将函数名作为参数传递 def outer() ...

  2. IE6浏览器有哪些常见的bug,缺陷或者与标准不一致的地方,如何解决

    IE6不支持min-height,解决办法使用css hack: .target { min-height: 100px; height: auto !important; height: 100px ...

  3. js登录,回车登录

    $(document).ready(function(){ $("#loginBtn").click(doLoginEvent); loadCookies(); //回车登录 do ...

  4. Safari 3D transform变换z-index层级渲染异常

    (猛戳来源:http://www.zhangxinxu.com/wordpress/?p=5569)

  5. mybatis choose标签的使用

    MyBatis 提供了 choose 元素.if标签是与(and)的关系,而 choose 是或(or)的关系. choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立 ...

  6. 重新设置Eclipse的workspace路径

    有3中方法可以更改workspace的路径设置: 1. 启动Eclipse/MyEclipse后, 打开"Window -> Preferences -> General -&g ...

  7. 设置元素text-overflow: ellipsis后引起的文本对齐问题

    .ellipsis { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } 给元素设置了这个属性之后,该行内元素和旁边的 ...

  8. html学习中

    Html常用标签一 <body style="background-color:red;"> Body 标签 Style 属性 background-color:red ...

  9. 转:window与linux互相拷贝文件

    window与linux互相拷贝文件   借助 PSCP 命令可以实现文件的互拷:     1.下载pscp.exe 文件 (我的资源文件中有)   2.如果想在所有目录可以执行,请更改环境变量. w ...

  10. ES6(Decorator(修饰器))

    Decorator(修饰器) 1.基本概念 函数用来修改 类 的行为 1.Decorator 是一个函数 2.通过Decorator(修饰器)能修改 类 的行为(扩展 类 的功能)3.Decorato ...