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


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


ObjectInputStream 类的 readField() 方法用于从流中读取持久字段并使其按名称可用。

用法

public ObjectInputStream.GetField readFields()throws IOException, ClassNotFoundException

参数

没有传递参数。

返回

表示被反序列化对象的持久字段的 GetField 对象

抛出

ClassNotFoundException - 如果找不到序列化对象的类。

IOException - 如果发生 I/O 错误。

NotActiveException - 如果流当前未读取对象。

例子1

import java.io.*;
import static java.lang.System.out;

public class ObjectInputStreamreadFieldExample1 {
   public static void main(String[] args) {
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream outstream = new FileOutputStream("file1.txt");
         ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);

         // write something in the file
         objoutstream.writeObject(new Task());
         objoutstream.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream objinstream = new ObjectInputStream(new FileInputStream("file1.txt"));

         // read the object and print the string
          Task  a = (Task) objinstream.readObject();

         // print the string that is in Example class
         System.out.println("" + a.st);

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static class Task implements Serializable {
      String st = "Welcome to javaTpoint";

      private String readObject(ObjectInputStream in) 
         throws IOException, ClassNotFoundException {

         // call readFields in readObject
         ObjectInputStream.GetField g = in.readFields();

         // save the string and return it
         return (String) g.get("st", null);
      }
   }
}

输出:

Welcome to javaTpoint

例子2

import java.io.*; 
  public class ObjectInputStreamreadFieldExample2 
{ 
    public static void main(String[] args) 
    { 
        String str1 = "JavaTpoint"; 
        try
        { 
            // create a new file with an ObjectOutputStream and ObjectInputStream 
            FileOutputStream fileout = new FileOutputStream("file1.txt"); 
            ObjectOutputStream filein = new ObjectOutputStream(fileout); 
            ObjectInputStream InStream  = new ObjectInputStream(new FileInputStream("file1.txt")); 
            // These are ObjectOutputStream methods, Don't bother about these methods 
            // As they are covered in other articles you can refer them 
            filein.writeObject(new Test()); 
            filein.writeUTF(str1); 
            filein.flush(); 
  
            // read the object and print the string 
            Test a = (Test) InStream.readObject(); 
  
            // String value using readFields() methods:
            System.out.println("Using readFields():" + a.str); 
  
            // Checking th use of registerValidation 
            System.out.println("----------------------------------------------------------"); 
            if (Test.str.equals("Welcome to javaTpoint")) 
            { 
                System.out.println("javaTpoint"); 
            } 
            
        } 
  
        catch (Exception excpt) 
        { 
            System.out.println("Error"); 
            excpt.printStackTrace(); 
        } 
    } 
  
    static class Test implements Serializable, ObjectInputValidation 
    { 
        static String str = "Welcome to javaTpoint";
        private String readObject(ObjectInputStream in) 
        throws IOException, ClassNotFoundException 
        { 
  
            // use of readFields() method:
            ObjectInputStream.GetField getfield = in.readFields(); 
  
            // USe of registerValidation() method:
            in.registerValidation(this, 0); 
  
            // Returning String "geek" to the main 
            return (String) getfield.get("str", null); 
        } 
  
        @Override
        public void validateObject() throws InvalidObjectException 
        { 
            throw new UnsupportedOperationException("Not supported yet."); 
        } 
    } 
}

输出:

Using readFields():Welcome to javaTpoint
----------------------------------------------------------
javaTpoint




相关用法


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