欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ObjectInputStream 和 ObjectOutputStream 介绍_动力节点Java学院整理

程序员文章站 2024-02-21 13:24:22
objectinputstream 和 objectoutputstream 的作用是,对基本数据和对象进行序列化操作支持。 创建“文件输出流”对应的objectoutp...

objectinputstream 和 objectoutputstream 的作用是,对基本数据和对象进行序列化操作支持。

创建“文件输出流”对应的objectoutputstream对象,该objectoutputstream对象能提供对“基本数据或对象”的持久存储;当我们需要读取这些存储的“基本数据或对象”时,可以创建“文件输入流”对应的objectinputstream,进而读取出这些“基本数据或对象”。

注意: 只有支持 java.io.serializable 或 java.io.externalizable 接口的对象才能被objectinputstream/objectoutputstream所操作!

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)

演示程序

源码如下(objectstreamtest.java):

/**
 * objectinputstream 和 objectoutputstream 测试程序
 *
 * 注意:通过objectinputstream, objectoutputstream操作的对象,必须是实现了serializable或externalizable序列化接口的类的实例。
 *
 * 
 */
import java.io.fileinputstream;  
import java.io.fileoutputstream;  
import java.io.objectinputstream;  
import java.io.objectoutputstream;  
import java.io.serializable;  
import java.util.map;
import java.util.hashmap;
import java.util.iterator;
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();
    }
  }
}
class 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+") ]";
  }
}

运行结果:

boolean:true
byte:65
char:a
int:20131015
float:3.140000
double:1.414000
two  -- green
one  -- red
three -- blue
box: [desk: (80, 48) ]

以上所述是小编给大家介绍的objectinputstream 和 objectoutputstream 的相关知识,希望对大家有所帮助