Home > Back-end >  Java how to judge whether a file to save the object serialization information?
Java how to judge whether a file to save the object serialization information?

Time:01-10

application purpose: to retrieve a file is stored in the folder a class object serialization information;
Problems : with ObjectInputStream (InputStream is) method reads to the file does not contain the serialization information application throws StreamCorruptedException abnormal
hope to get help: how to judge whether a file stores the serialization information?
 StudentSerialization stuseria=new StudentSerialization (); 
ObjectInputStream ois=null;
Try {
The File File=new File (" D: \ \ the Ideal \ \ practice \ \ ");
Stuseria. SearchFile (file);//add the file folder all the files to the stuseria. The filelist collection
If (stuseria. The filelist. The size ()!=0) {
Int num=0;//the number of students object to find
For (File f: stuseria filelist) {
Ois=new ObjectInputStream (new FileInputStream (f));//throw an exception where
The Object o;
If ((o=ois. ReadObject ()) instanceof Student) {
//omitted to obtain objects related processing code
}
}
}
//omission related catch/finally and close the flow code block

CodePudding user response:

Throw an exception where add a try-catch; The catch by StreamCorruptedException will continue into the next cycle; Problem solved
The modified code is as follows:
 for (File f: stuseria filelist) {
Try {
Ois=new ObjectInputStream (new FileInputStream (f));//read to store the serialized file, throws StreamCorruptedException abnormal
} the catch (StreamCorruptedException e) {
continue;
}

CodePudding user response:

Files are identified,

Java serialization - binary format of the rites, rounding _ column - CSDN blog _java binary serialization
https://blog.csdn.net/lqzkcx3/article/details/79463450
  • Related