ObjectInputStream readObjectOverride()方法

java.io.ObjectInputStream.readObjectOverride() 方法由ObjectOutputStream受信任子类使用受保护的无参数构造函数构造对象输出流。该子类预计将提供一个覆盖方法的修饰符“final”。

1 语法

protected Object readObjectOverride()

2 参数

3 返回值

返回从流读取的Object。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.ObjectInputStream.readObjectOverride()方法的例子
 */
import java.io.*;

public class Demo extends ObjectInputStream{

    public Demo(InputStream in) throws IOException {
        super(in);
    }

    public static void main(String[] args) {
        String s = "Hello World";

        try {
            // create a new file with an ObjectOutputStream
            FileOutputStream out = new FileOutputStream("test.txt");
            ObjectOutputStream oout = new ObjectOutputStream(out);

            // write something in the file
            oout.writeObject(s);
            oout.flush();

            // create an ObjectInputStream for the file we created before
            Demo ois = new Demo(new FileInputStream("test.txt"));

            // read and print an object and cast it as string
            System.out.println("" + (String)ois.readObjectOverride());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

null

热门文章

优秀文章