我与用户之间有一个空的file.txt,并且我要使用ObjectOutputStream将对象写入文件之后,要将此元素添加到HashSet中。我该怎么做? 这是我添加元素的方法:
Set<User> users = new HashSet<User>();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNext()){
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\|");
String name = lineScanner.next();
String password = lineScanner.next();
String email = lineScanner.next();
String nameSurname = lineScanner.next();
User user = new User(name, password, email, nameSurname);
users.add(user);