这是我的保存方法
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
逐行读取文件,并使用与在toString
类的Person
中使用的定界符相同的分隔符。
例如:假设您使用" "
作为分隔符。