当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ObjectInputStream readObjectOverride()用法及代码示例


ObjectInputStream 类的 readObjectOverride() 方法由使用受保护的 no-arg 构造函数构造 ObjectOutputStream 的 ObjectOutputStream 的可信子类调用。它返回一个对象。

用法

protected Object readObjectOverride()throws IOException , ClassNotFoundException

参数

没有传递参数。

返回

从流中读取的对象。

抛出

类未找到异常

可选数据异常

IOException

例子1

import java.io.*;
public class ObjectInputStreamreadObjectOverridebExample1 extends ObjectInputStream{
   public ObjectInputStreamreadObjectOverridebExample1(InputStream in) throws IOException {
      super(in);
    }
    
   public static void main(String[] args) {
      String s = "JavaTpoint";
      
      try {
         FileOutputStream outstream = new FileOutputStream("file1.txt");
         ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
         objoutstream.writeObject(s);
         objoutstream.flush();
         ObjectInputStreamreadObjectOverridebExample1 ois = new       ObjectInputStreamreadObjectOverridebExample1(new FileInputStream("file1.txt"));
         System.out.println("" + (String)ois.readObjectOverride());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

输出:

null

例子2

import java.io.*; 
   class ObjectInputStreamDemo extends ObjectInputStream 
{ 
  
    public ObjectInputStreamDemo(InputStream in) throws IOException 
    { 
        super(in); 
    } 
} 
public class ObjectInputStreamreadObjectOverridebExample2 extends ObjectInputStream
{ 
    public ObjectInputStreamreadObjectOverridebExample2(InputStream in) throws IOException {
      super(in);
    }
    public static void main(String[] args) 
    { 
        
        String str = "JavaTpoint"; 
        try
        { 
         
            FileOutputStream outstream = new FileOutputStream("file1.txt"); 
            ObjectOutputStream objinstream = new ObjectOutputStream(outstream); 
  
            ObjectInputStream InStream  = new ObjectInputStream(new FileInputStream("file1.txt")); 
 
            objinstream.writeObject(new Test()); 
            objinstream.writeUTF(str); 
            objinstream.flush(); 
  
            Test a = (Test) InStream.readObject(); 
            ObjectInputStreamreadObjectOverridebExample2 ois = new ObjectInputStreamreadObjectOverridebExample2(new FileInputStream("file1.txt"));

  
          System.out.println("" + (String)ois.readObjectOverride());
          System.out.println("" + str);
           
        } 
  
        catch (Exception excpt) 
        { 
            System.out.println("Error"); 
            excpt.printStackTrace(); 
        } 
    } 
  
    static class Test implements Serializable
    { 
       static String ss = "Welcome to javaTpoint"; 
        private String readObject(ObjectInputStream in) 
        throws IOException, ClassNotFoundException 
        { 
                       ObjectInputStream.GetField getfield = in.readFields(); 
  
            return (String) getfield.get("ss", null); 
        } 
  
        
    } 
}

输出:

null
JavaTpoint




相关用法


注:本文由纯净天空筛选整理自 Java ObjectInputStream readObjectOverride() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。