ObjectInputStream 和 ObjectOutputStream 的作用是,對(duì)基本數(shù)據(jù)和對(duì)象進(jìn)行序列化操作支持。
創(chuàng)建“文件輸出流”對(duì)應(yīng)的ObjectOutputStream對(duì)象,該ObjectOutputStream對(duì)象能提供對(duì)“基本數(shù)據(jù)或?qū)ο?rdquo;的持久存儲(chǔ);當(dāng)我們需要讀取這些存儲(chǔ)的“基本數(shù)據(jù)或?qū)ο?rdquo;時(shí),可以創(chuàng)建“文件輸入流”對(duì)應(yīng)的ObjectInputStream,進(jìn)而讀取出這些“基本數(shù)據(jù)或?qū)ο?rdquo;。
注意: 只有支持 java.io.Serializable 或 java.io.Externalizable 接口的對(duì)象才能被ObjectInputStream/ObjectOutputStream所操作!
ObjectOutputStream 函數(shù)列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
// 構(gòu)造函數(shù) ObjectOutputStream(OutputStream output) // public函數(shù) 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 函數(shù)列表 // 構(gòu)造函數(shù) 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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/** * ObjectInputStream 和 ObjectOutputStream 測(cè)試程序 * * 注意:通過(guò)ObjectInputStream, ObjectOutputStream操作的對(duì)象,必須是實(shí)現(xiàn)了Serializable或Externalizable序列化接口的類的實(shí)例。 * * */ 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 測(cè)試函數(shù) */ 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); // 寫(xiě)入HashMap對(duì)象 HashMap map = new HashMap(); map.put( "one" , "red" ); map.put( "two" , "green" ); map.put( "three" , "blue" ); out.writeObject(map); // 寫(xiě)入自定義的Box對(duì)象,Box實(shí)現(xiàn)了Serializable接口 Box box = new Box( "desk" , 80 , 48 ); out.writeObject(box); out.close(); } catch (Exception ex) { ex.printStackTrace(); } } /** * ObjectInputStream 測(cè)試函數(shù) */ 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對(duì)象 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對(duì)象,Box實(shí)現(xiàn)了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+ ") ]" ; } } |
運(yùn)行結(jié)果:
1
2
3
4
5
6
7
8
9
10
|
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 的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!