Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tutorial 46 Serializing Arrays
- //no idea whats going on
- //---------------------------------------------------------------------------------------------------------------------------------------
- //Person.java
- import java.io.Serializable;
- public class Person implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 4801633306273802062L;
- private int id;
- private String name;
- public Person(int id, String name) {
- this.id = id;
- this.name = name;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + "]";
- }
- //---------------------------------------------------------------------------------------------------------------------------------------
- //WriteObjects.java
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class WriteObjects {
- public static void main(String[] args) {
- System.out.println("Writing objects...");
- Person[] people = {new Person(1, "Sue"), new Person(99, "Mike"), new Person(7, "Bob")};
- ArrayList<Person> peopleList = new ArrayList<Person>(Arrays.asList(people));
- try(FileOutputStream fs = new FileOutputStream("test.ser")) {
- ObjectOutputStream os = new ObjectOutputStream(fs);
- // Write entire array
- os.writeObject(people);
- // Write arraylist
- os.writeObject(peopleList);
- // Write objects one by one
- os.writeInt(peopleList.size());
- for(Person person: peopleList){
- os.writeObject(person);
- }
- os.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //---------------------------------------------------------------------------------------------------------------------------------------
- //ReadObjects.java
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.util.ArrayList;
- public class ReadObjects {
- public static void main(String[] args){
- System.out.println("Reading Objects...");
- try(FileInputStream fi = new FileInputStream("test.ser")) {
- ObjectInputStream os = new ObjectInputStream(fi);
- // Read entire array
- Person[] people = (Person[])os.readObject();
- // Read entire arraylist
- @SuppressWarnings("unchecked") //Suppresses warnings
- ArrayList<Person> peopleList = (ArrayList<Person>)os.readObject();
- for(Person person: people) {
- System.out.println(person);
- }
- for(Person person: peopleList) {
- System.out.println(person);
- }
- // Read objects one by one.
- int num = os.readInt();
- for(int i=0; i<num; i++) {
- Person person = (Person)os.readObject();
- System.out.println(person);
- }
- os.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- //---------------------------------------------------------------------------------------------------------------------------------------
- //if you run the ReadObjects.java tab this should be the run result:
- /*
- Reading Objects...
- Person [id=1, name=Sue]
- Person [id=99, name=Mike]
- Person [id=7, name=Bob]
- Person [id=1, name=Sue]
- Person [id=99, name=Mike]
- Person [id=7, name=Bob]
- Person [id=1, name=Sue]
- Person [id=99, name=Mike]
- Person [id=7, name=Bob]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement