当前位置:天才代写 > tutorial > JAVA 教程 > java io进修(四) ObjectInputStream 和 ObjectOutputStream

java io进修(四) ObjectInputStream 和 ObjectOutputStream

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:55

副标题#e#

本章,我们进修ObjectInputStream 和 ObjectOutputStream

ObjectInputStream 和 ObjectOutputStream 先容

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)

查察本栏目


#p#副标题#e#

演示措施

源码如下(ObjectStreamTest.java):

/**
 * ObjectInputStream 和 ObjectOutputStream 测试措施
 *
 * 留意:通过ObjectInputStream, ObjectOutputStream操纵的工具,必需是实现了Serializable或Externalizable序列化接口的类的实例。
 *
 * @author skywang
 */
     
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) ]

来历:http://www.cnblogs.com/skywang12345/p/io_05.html

 

    关键字:


天才代写-代写联系方式