Java uses ObjectInputStream to read objects from a file

Source: Internet
Author: User

Import java.io.EOFException;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.ObjectInputStream;


public class Main {

/**
*objectinputstream Use Example
*/
public void readpersons (String filename) {

ObjectInputStream inputstream = null;

try {

Constructing ObjectInputStream objects
InputStream = new ObjectInputStream (new FileInputStream (filename));

Object obj = null;

while ((obj = Inputstream.readobject ()) = null) {

if (obj instanceof person) {

System.out.println ((person) obj). toString ());
}

}

} catch (Eofexception ex) {//trigger this exception at the end of the read to file
System.out.println ("End of File reached.");
} catch (ClassNotFoundException ex) {
Ex.printstacktrace ();
} catch (FileNotFoundException ex) {
Ex.printstacktrace ();
} catch (IOException ex) {
Ex.printstacktrace ();
} finally {
Close the ObjectInputStream
try {
if (InputStream! = null) {
Inputstream.close ();
}
} catch (IOException ex) {
Ex.printstacktrace ();
}
}
}

/**
* @param args the command line arguments
*/
public static void Main (string[] args) {
New Main (). Readpersons ("MyFile.txt");
}
}

ReadObject Method:
Public final Object ReadObject () throws Ioexception,classnotfoundexception

Reads an object from ObjectInputStream. The class of the object, the signature of the class, and the values of all non-transient and non-static fields of the class and all its super-types are read. You can use the WriteObject and ReadObject methods to override the default deserialization for a class. The objects referenced by this object are transitive read, so that ReadObject can reconstruct the fully equivalent graph of those objects.
Completely restores the root object by fully restoring all the fields of the root object and the objects it references. At this point, the order of execution of the object validation callback is based on its registration priority. Callbacks are registered by the object (according to ReadObject specific methods) at the time of its individual restore. An exception is thrown when a problem occurs in InputStream or when a class that should not be deserialized is encountered. All exceptions are fatal to InputStream, leaving them in an indeterminate state, which continues until the caller ignores or resumes the stream state.
Designated by:
ReadObject in the interface objectinput
Return:
Objects read from the stream
Thrown:
ClassNotFoundException-the class for the serialized object could not be found.
Invalidclassexception-the class used by serialization is out of the question.
Streamcorruptedexception-the control information in the stream is inconsistent.
Optionaldataexception-The base data is found in the stream instead of in the object.
IOException-Any general input/output related exceptions. This is the full description of ReadObject, cannot read the correct object information, the error will not return NULL to prompt you

Reference: http://www.debugease.com/j2ee/171334.html

Java uses ObjectInputStream to read objects from a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.