如何使用ObjectInputStream从文件向HashSet添加对象?

时间:2018-11-23 11:01:34

标签: java io set

我与用户之间有一个空的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);

0 个答案:

没有答案