java将arraylist写入txt_Java-如何将文件读入对象的ArrayList?

我想制作一个ArrayList的Student并将其保存到文件中以备后用.我成功编写了它,但是当我将其读回ArrayList时,我只有一个对象.

public class Student implements Serializable{

public String fname, lname, course;

int section;

private static final long serialVersionUID = 1L;

public static ArrayList students = getStudent();

public Student() {

}

public Student(String fname, String lname, String course, int section){

this.fname = fname;

this.lname = lname;

this.course = course;

this.section = section;

}

public static void addStudent(){

String fname = GetInput.getInput("Enter the First Name: ");

String lname = GetInput.getInput("Enter the Last Name: ");

String course = GetInput.getInput("Enter the Course: ");

String S_section = GetInput.getInput("Enter the section: ");

int section = Integer.parseInt(S_section);

Student student = new Student(fname, lname, course, section);

students.add(student);

System.out.println("Writing to file...");

try {

writeToFile(student);

} catch (FileNotFoundException ex) {

System.out.println(ex.getMessage());

} catch (IOException ex) {

System.out.println(ex.getMessage());

}

}

public static ArrayList getStudent(){

try{

FileInputStream fis = new FileInputStream("C:\\students.ser");

ObjectInputStream ois = new ObjectInputStream(fis);

ArrayList students1 = (ArrayList) ois.readObject();

ois.close();

return students1;

} catch( ClassNotFoundException | IOException ex){

System.out.println(ex.getMessage());

return null;

}

}

public static void listStudent(ArrayList students){

System.out.println("View the Records in the Database:");

for(Student student: students){

System.out.println("Name: " + student.fname + " " + student.lname);

System.out.println("Course: " + student.course);

System.out.println("Section: " + student.section);

System.out.println();

}

}

static void writeToFile(Student student) throws FileNotFoundException, IOException{

String path = "C:\\students.ser";

FileOutputStream fos = new FileOutputStream(path, true);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(student);

oos.close();

System.out.println("New Record has been written!");

}

当我通过调用getStudent()读取文件并通过listStudent()将其打印出来时,我只有该文件的一条记录.

请帮我!

非常感谢.

编辑

我曾尝试将arraylist写入文件并将其读入arraylist.我会告诉你我是怎么做到的.

首先,我将arraylist写入文件:

public static ArrayList students = new ArrayList<>();

public static void addStudent(){

Student student = new Student(fname, lname, course, section);

students.add(student);

System.out.println("Writing to file...");

try {

writeToFile(students);

}catch...

}

static void writeToFile(ArrayList students) throws FileNotFoundException, IOException{

String path = "C:\\students.ser";

FileOutputStream fos = new FileOutputStream(path, true);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(students);

oos.close();

System.out.println("New Record has been written!");

然后我阅读了学生档案:

public static ArrayList getStudent(){

try{

FileInputStream fis = new FileInputStream("C:\\students.ser");

ObjectInputStream ois = new ObjectInputStream(fis);

ArrayList students1 = (ArrayList) ois.readObject();

ois.close();

return students1;

} catch( ClassNotFoundException | IOException ex){

System.out.println(ex.getMessage());

return null;

}

}

我看到文件中有许多对象,因为文件大小不断增长.但是读取后我只有一个对象,这是我写入文件的第一个对象.

解决方法:

您发表评论:

Thanks for your comment. I noticed that, however I appended the new object to the old file, so technically I have bunch of objects in my file. FileOutputStream fos = new FileOutputStream(path, true);

尽管从技术上讲,它确实会附加到文件的末尾,并且可以很好地与文本文件一起使用,但我真的认为这在序列化方面不会起作用或不能很好地工作.我猜想要附加序列化,您首先必须从文件中读取所有对象,然后再编写而不通过序列化机制附加所有对象.如果我是我,我会重新编写您的输入和输出代码.

编辑

我担心您有太多不同的东西都塞在一个类中,从而使程序混乱且难以调试.一些一般建议可帮助清理此任务:

>首先创建一个名为Student的类-您已完成此操作-但要使其成为一个纯Student类,具有私有的名字,姓氏,部分和课程字段,这些字段的getter和setter(您需要这些),合适的构造函数(我想你已经知道了).

>给它一个体面的公共String toString()方法,该方法返回一个保存对象字段值的String.

>从Student中获取所有其他内容,所有静态方法,所有ArrayLists,用于写入或读取文件的任何代码.

>创建另一个类,称为StudentCollection

>给它一个私有的非静态ArrayList< Student>.场上,说叫学生.

>给它添加一个addStudent(Student student)方法,该方法允许外部类将Student对象添加到此类.

>给它提供一个公共String toString()方法,该方法返回列表的toString(),即return students.toString();.

>给它提供一个公共无效的readFromFile(File file)方法,该方法使用序列化来读取ArrayList< Student>.从文件.

>给它一个公共的void writeToFile(File file)方法,该方法使用序列化来写ArrayList< Student>.到文件.

>最后,创建一个TestStudent类,它仅具有一个方法,即公共静态void main方法.

>在主要中,创建一个StudentCollection对象.

>使用您的addStudent(…)方法向学生填充它.

>创建一个File对象,并调用传入文件中的writeToFile(…).

>然后测试从同一文件读取…

例如,main方法看起来几乎类似于下面的代码.请注意,尽管在我的测试用例中证明了这一点是可行的,但我创建了一个简化的Student类,该类仅使用2个参数来使用名字和姓氏.您的代码显然将使用更多参数.

import java.io.*;

import java.util.ArrayList;

import java.util.List;

public class StudentTest {

private static final String DATA_FILE_PATH = "myFile.dat";

public static void main(String[] args) {

Student[] students = {new Student("John", "Smith"),

new Student("Mike", "Barnes"),

new Student("Mickey", "Mouse"),

new Student("Donald", "Duck")};

// create our collection object

StudentCollection studentColl1 = new StudentCollection();

// print out that it currently is empty

System.out.println("studentColl1: " + studentColl1);

// Add Student objects to it

for (Student student : students) {

studentColl1.addStudent(student);

}

// show that it is now full

System.out.println("studentColl1: " + studentColl1);

// create a file

File myFile = new File(DATA_FILE_PATH);

// write out our collection to file on disk

studentColl1.writeToFile(myFile);

// create another collection object

StudentCollection studentColl2 = new StudentCollection();

// show that it is empty

System.out.println("studentColl2: " + studentColl2);

// read the list back into the new StudentCollection object

File myFile2 = new File(DATA_FILE_PATH);

studentColl2.readFromFile(myFile2);

// add a few more Student's:

studentColl2.addStudent(new Student("Stack", "Overflow"));

studentColl2.addStudent(new Student("Donald", "Trump"));

// show the result

System.out.println("studentColl2: " + studentColl2);

}

}

标签:arraylist,java

来源: https://codeday.me/bug/20191122/2060037.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值