0

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?

2
  • 1
    You need to have logic around it. Write a toString for the class and store the toString in the file and then get the toStringValue from the file and reconstruct your class. Commented Jan 19, 2017 at 16:21
  • 2
    You could serialize and deserialize it into an, for example, json string and write/read it into/from the file. Commented Jan 19, 2017 at 16:22

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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);
   }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.