4
I wanted to work with serialization in java files and I read this tutorial about how to write the object in the file and this one on the how to read the file object
Then I created the following class that saves the Adreess object in the file "file.ser":
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import wl.revisao.write.Address;
public class WriteObject {
public void serializeAddressJDK7(Address address) {
try (ObjectOutputStream oos
= new ObjectOutputStream(new FileOutputStream("arquivo.ser",true))) {
oos.writeObject(address);
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Address deserialzeAddressJDK7(String filename) {
Address address = null;
try (ObjectInputStream ois
= new ObjectInputStream(new FileInputStream(filename))) {
address = (Address) ois.readObject();
} catch (Exception ex) {
ex.printStackTrace();
}
return address;
}
//Method indicated by friend Isac with the answer below
public List<Address> recuperarTodos() {
List<Address> addresses = new ArrayList<Address>(); //lista de Address
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("arquivo.res"))) {
while (true) {
Address address = new Address();
address = (Address) ois.readObject(); //le outro objeto e adicionar à lista
addresses.add(address); //le outro objeto e adicionar à lista
}
} catch (EOFException ex) {
} catch (Exception ex) {
ex.printStackTrace();
}
return addresses;
}
}
Here’s my main class:
public class Main {
public static void main(String args[]) {
WriteObject obj = new WriteObject();
Address address = new Address();
address.setStreet("Wall street");
address.setCountry("United States Of America");
Address address2 = new Address();
address2.setStreet("Avenida Brasil");
address2.setCountry("Brasil");
obj.serializeAddressJDK7(address);
obj.serializeAddressJDK7(address2);
// obj.deserialzeAddressJDK7("arquivo.ser");
System.out.println(obj.recuperarTodos());
}
}
The other method deserialAddressJDK7 returns an Address object read from the file.
OK everything is perfect!! The problem is when I save more than one object in the file ".ser file" because the method was built to read only one object saved in the file;
How do I return a list of objects saved in that file?
Last, the infamous Address class:
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
String street;
String country;
public Address() {
}
public void setStreet(String street) {
this.street = street;
}
public void setCountry(String country) {
this.country = country;
}
public String getStreet() {
return this.street;
}
public String getCountry() {
return this.country;
}
@Override
public String toString() {
return new StringBuffer(" Street : ")
.append(this.street).append(" Country : ")
.append(this.country).toString();
}
}
ps: I’m using the append as true, because as we know I want to accumulate the objects in the file.ser , and if I omitted it, this would not be possible!
If there is another way to save the objects in the files without using the append , and it is possible to list these files, I would be happy to know.
I used the Boolean append with true , because it was the only alternative I had... I didn’t know how to rewrite the file without deleting the previously existing objects!
@Thanks a guy I’ll take a look!
– Pena Pintada