簡體   English   中英

Java-如何將ArrayList保存到文件中並打開它?

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

我開設了一個名為“主題”的課程。 該類包括以下幾行:

    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重新打開。 有什么辦法嗎?

您可以使用Java序列化和反序列化或Jackson API進行存儲和檢索。

一個小例子來自: Java序列化

這些示例的注釋很小:我保留了原始示例。 請始終在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);
   }
}

您可以使用序列化和反序列化將其寫入文件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM