Java - 如何将ArrayList保存到文件中并打开它?

时间:2017-01-19 16:19:34

标签: java file arraylist

我创建了一个名为Subject的课程。该课程由以下几行组成:

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

我正在尝试创建两种方法,用于将Subject的ArrayList保存到文件中,并将其重新打开为Subject的ArrayList。 任何解决方案?

3 个答案:

答案 0 :(得分:0)

您可以使用Java序列化和反序列化或Jackson API来存储和检索。

答案 1 :(得分:0)

来自Java serialization

的一个小例子

这些例子的一个非常小的说明:我保留了原始的例子。 请始终在finally子句中关闭文件和流!

创建对象:

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

将对象写入文件:

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

要获取列表:

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

答案 2 :(得分:0)

您可以使用序列化和反序列化将其写入文件:

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

然后你可以再次阅读并投下它:

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