Java ObjectInputStream registerValidation()方法与示例

ObjectInputStream类registerValidation()方法 (ObjectInputStream Class registerValidation() method)

  • registerValidation() method is available in java.io package.

    registerValidation()方法在java.io包中可用。

  • registerValidation() method is used to register an object (ob) to be validated before the graph is returned.

    registerValidation()方法用于在返回图形之前注册要验证的对象(ob)。

  • registerValidation() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    registerValidation()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • registerValidation() method may throw an exception at the time of registering an object to validate.

    registerValidation()方法在注册要验证的对象时可能会引发异常。

    • NotActiveException: This exception may throw when the given parameters are not allowed to enable.NotActiveException :如果不允许启用给定参数,则可能引发此异常。
    • InvalidObjectException: This exception may throw when the given parameter is not valid.InvalidObjectException :当给定参数无效时,可能引发此异常。

Syntax:

句法:

    public void registerValidation(ObjectInputValiadation ob, int priority);

Parameter(s):

参数:

  • ObjectInputValiadation ob – represents the object to get the validation callback.

    ObjectInputValiadation ob –表示要获取验证回调的对象。

  • int priority – represents the priority by which based on ordering of callback.

    int priority –表示基于回调顺序的优先级。

Return value:

返回值:

The return type of the method is void, it returns nothing.

该方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example 
// of void registerValidation(ObjectInputValiadation ob, 
// int priority) method of ObjectInputStream


import java.io.*;

public class RegisterValidation {
 public static void main(String[] args) {
  try {
   // Instantiates ObjectOutputStream , ObjectInputStream 
   // FileInputStream and FileOutputStream
   FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
   Drain obj_out_stm = new Drain(file_out_stm);
   FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
   ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

   obj_out_stm.writeObject(new ReadObject());

   // By using readObject() method is to read
   ReadObject a = (ReadObject) obj_in_stm.readObject();

   // Testing Validations
   if (ReadObject.str.equals("Java Programmer")) {
    System.out.println("Validations Successful!!!");
   } else {
    System.out.println("Invalid Validations!!! ");
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 static class ReadObject implements Serializable, ObjectInputValidation {
  static String str = "Java World!!!";
  private String readObject(ObjectInputStream ois)
  throws IOException, ClassNotFoundException {
   // By using readFields() method is to get fields 
   ObjectInputStream.GetField get_f = ois.readFields();

   ois.registerValidation(this, 0);

   // Return str
   return (String) get_f.get("str", null);
  }

  @Override
  public void validateObject() throws InvalidObjectException {
   throw new UnsupportedOperationException("Unsupported Validations!!!");
  }
 }
}

Output

输出量

Invalid Validations!!!


翻译自: https://www.includehelp.com/java/objectinputstream-registervalidation-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值