I would like to serialize and de-serialize and read and write objects to the file.I can do additional writing, but I can't read it well.
No matter how many times you load it, you can only get the first object written to the file.
I want to load all objects that I wrote to the file.
I thought of turning the loop with the main function, and I used RandomAccessFile to move the file pointer, but it didn't work.
There was also a way to read one line at a time, but if the object has a different type, an error will occur.
If anyone knows how to do it well, please let me know.
Thank you for your cooperation.
Member.java File
package hello;
import java.io.Serializable;
public class member implements Serializable {
public String name;// Name
public age;// age
}
SerializableSampleTest.java File
package hello;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class SerializableSampleTest{
public static void main(String[]args) {
SerializableSampleTest1 = new SerializableSampleTest();
SerializableSampleTest2 = new SerializableSampleTest();
// first person
Member mb1 = new Member();
mb1.name="sakura";
mb1.age = 20;
List Akumi = new ArrayList();
Akumi.add (mb1.name);
Akumi.add(mb1.age);
sst1.write(Akumi);
// second person
Member mb2 = new Member();
mb2.name="yuri";
mb2.age = 19;
List Bkumi = new ArrayList();
Bkumi.add (mb2.name);
Bkumi.add(mb2.age);
sst2.write(Bkumi);
System.out.println(sst1.read());
System.out.println(sst2.read());
}
public void write(java.util.List<Member>Mem){
// serialization
try(ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream("member", true))){
oos.writeObject(Mem);
System.out.println("Serialization Complete";
oos.close();
} catch(IOExceptione){
System.out.println("error");
}
}
public java.util.List<Member>read(){
java.util.List<Member>Mem=null;
// de-serialization
try(ObjectInputStream ois=newObjectInputStream(newFileInputStream("member")){
Mem=(List<Member>)ois.readObject();
System.out.println("Decialization Complete";
ois.close();
} catch(IOException | ClassNotFoundException) {
System.out.println("error");
}
return Mem;
}
}
Is it like this?
public class SerializableSampleTest{
try(ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream("member")){
// first person
Member mb1 = new Member();
mb1.name="sakura";
mb1.age = 20;
oos.writeObject(mb1);
// second person
Member mb2 = new Member();
mb2.name="yuri";
mb2.age = 19;
oos.writeObject(mb2);
System.out.println("Serialization Complete";
} catch(IOExceptione){
System.out.println("error");
}
try(ObjectInputStream ois=newObjectInputStream(newFileInputStream("member")){
while(true){
Member mem=(Member)ois.readObject();
System.out.println(mem);
}
} catch(EOFExceptione){
System.out.println("Decialization Complete";
} catch(IOException | ClassNotFoundException) {
System.out.println("error");
}
}
First of all, the List
is used incorrectly.Please check again.
You do not need to use List
.
Also, ObjectOutputStream
is AutoCloseable
, so you don't need to close()
in that block if you use the try-with-resources statement.
Also, you do not need to open two ObjectOutputStream
to writeObject()
.
The same goes for readObject()
.
It's better not to open it.
You can determine if readObject()
has read all objects, because FileInputStream
is wrapped, you can use EOFException
.
© 2024 OneMinuteCode. All rights reserved.