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 ObjectInputStreamimport java.io.*;public class RegisterValidation {public static void main(String[] args) {try {// Instantiates ObjectOutputStream , ObjectInputStream // FileInputStream and FileOutputStreamFileOutputStream 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 readReadObject a = (ReadObject) obj_in_stm.readObject();// Testing Validationsif (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 strreturn (String) get_f.get("str", null);}@Overridepublic void validateObject() throws InvalidObjectException {throw new UnsupportedOperationException("Unsupported Validations!!!");}} }Output
輸出量
Invalid Validations!!!翻譯自: https://www.includehelp.com/java/objectinputstream-registervalidation-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java ObjectInputStream registerValidation()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Process getOutp
- 下一篇: List 去重的 6 种方法,这个方法最