【问题标题】:Java - How to save an ArrayList into a file and open it?Java - 如何将 ArrayList 保存到文件中并打开它?
【发布时间】:2017-06-04 10:34:51
【问题描述】:

我创建了一个名为 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;
    }
}

我正在尝试创建两种方法来将主题的 ArrayList 保存到文件中并将其作为主题的 ArrayList 重新打开。 有什么解决办法吗?

【问题讨论】:

  • 你需要有逻辑。为类编写一个 toString 并将 toString 存储在文件中,然后从文件中获取 toStringValue 并重建您的类。
  • 您可以将其序列化和反序列化为例如 json 字符串并将其写入/读取文件。

标签: java file arraylist


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    一个小例子来自: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);
       }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 2017-03-25
        • 2012-04-11
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        相关资源
        最近更新 更多