如何读取文件并将其存储在对象数组列表中?

问题描述 投票:0回答:1

这是我的保存方法

public static void save() {
        try {
            PrintWriter myWriter = new PrintWriter("database.txt");
            for(int i=0; i<people.size(); i++) {
                myWriter.println(people.get(i).toString());
            }
            myWriter.close();
            System.out.println("Successfully wrote to the file.");
            menu();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

这是文件中的外观

    Donald     Trump  23323.00

这是数组列表的字段和名称

ArrayList<Person> people = new ArrayList<Person>();
public Person(String name, String password, double money) {
        this.name = name;
        this.password = password;
        this.money = money;
    }
constructors below.....

我如何读取该文件并将其存储在对象的数组列表中?需要帮助:D

java arraylist file-handling readfile
1个回答
0
投票

逐行读取文件,并使用与在toString类的Person中使用的定界符相同的分隔符。

例如:假设您使用" "作为分隔符。

© www.soinside.com 2019 - 2024. All rights reserved.