p32929
p32929

Reputation: 99

Java - How to save an ArrayList into a file and open it?

I made a class called Subject. The class consists of these lines:

    class Subject {
    int serial;
    Double credit, gpa, tgpa = 0.0;

    public Subject(int serial, Double credit, Double gpa, Double tgpa) {
        this.serial = serial;
        this.credit = credit;
        this.gpa = gpa;
        this.tgpa = tgpa;
    }

    public int getSerial() {
        return serial;
    }

    public void setSerial(int serial) {
        this.serial = serial;
    }

    public Double getCredit() {
        return credit;
    }

    public void setCredit(Double credit) {
        this.credit = credit;
    }

    public Double getGpa() {
        return gpa;
    }

    public void setGpa(Double gpa) {
        this.gpa = gpa;
    }

    public Double getTgpa() {
        return tgpa;
    }

    public void setTgpa(Double tgpa) {
        this.tgpa = tgpa;
    }
}

I'm trying to create two methods for Saving the ArrayList of Subject into a file and Reopen it as an ArrayList of Subject. Any solution?

Upvotes: 0

Views: 4769

Answers (3)

nick wouters
nick wouters

Reputation: 96

A small example from:Java serialization

A very small note with these examples: I kept the original example. Please always close your files and streams in the finally clause!

Create object:

public class Employee implements java.io.Serializable {
   public String name;
   public String address;
   public transient int SSN;
   public int number;

   public void mailCheck() {
      System.out.println("Mailing a check to " + name + " " + address);
   }
}

Write object to file:

import java.io.*;
public class SerializeDemo {

  public static void main(String [] args) {

     Employee e = new Employee();
     e.name = "Reyan Ali";
     e.address = "Phokka Kuan, Ambehta Peer";
     e.SSN = 11122333;
     e.number = 101;

     try {
        FileOutputStream fileOut =
        new FileOutputStream("/tmp/employee.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(e);
        out.close();
        fileOut.close();
        System.out.printf("Serialized data is saved in /tmp/employee.ser");
     }catch(IOException i) {
        i.printStackTrace();
     }
  }
}

To get the list back:

import java.io.*;
public class DeserializeDemo {

   public static void main(String [] args) {
      Employee e = null;
      try {
         FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i) {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c) {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }

      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
   }
}

Upvotes: 1

Christian
Christian

Reputation: 22343

You could use serialization and deserialization to write it into a file:

try (FileOutputStream fos = new FileOutputStream("serializedObject.txt"); 
     ObjectOutputStream oos = new ObjectOutputStream(fos)) {
      oos.writeObject(yourArrayList);
}

Then you can read it again and cast it:

ObjectInputStream ois =
                 new ObjectInputStream(new FileInputStream("serializedObject.txt"));
//Gets the object
ois.readObject();

Upvotes: 1

dassum
dassum

Reputation: 5093

You can either use Java Serialization and Deserialization or Jackson API to store and retrieve .

Upvotes: 0

Related Questions