Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象的arraylist添加到文件_Java - Fatal编程技术网

Java 如何将对象的arraylist添加到文件

Java 如何将对象的arraylist添加到文件,java,Java,我想写一个程序,有一个注册和登录按钮。如果用户按下注册按钮,则其用户名和密码必须写入文件。我在附加到文件时遇到问题。我有一个名为people的objectuser的arraylist,我将这个arraylist添加到我的文件中。但当我打印文件的内容时,我只看到第一个用户,如果我打印arraylist,它只有一个对象是最后一个。下面是我在类中的addmethod: public class AddToFile { ArrayList<User> people = new Ar

我想写一个程序,有一个注册和登录按钮。如果用户按下注册按钮,则其用户名和密码必须写入文件。我在附加到文件时遇到问题。我有一个名为people的objectuser的arraylist,我将这个arraylist添加到我的文件中。但当我打印文件的内容时,我只看到第一个用户,如果我打印arraylist,它只有一个对象是最后一个。下面是我在类中的addmethod:

public class AddToFile 
{
    ArrayList<User> people = new ArrayList<User>();
    User user;

    public void add(String username, String password) throws FileNotFoundException, IOException, ClassNotFoundException 
    {
        User u1 = new User(username, password, "hello", null, null, null);

        FileOutputStream fout = new FileOutputStream("f.txt", true);
        ObjectOutputStream out = new ObjectOutputStream(fout);
        people.add(u1);
        System.out.println(people.size());
        for (User people1 : people) 
        {
            System.out.println("user  " + people1.username + "  pass " + people1.password);
        }
        for (User p : people) 
        {
            out.writeObject(p);       
        }
        out.flush();
        out.close();

        System.out.println("success");

        ObjectInputStream is = new ObjectInputStream(new FileInputStream("f.txt"));
        User u = (User) is.readObject();
        System.out.println("File  user :" + u.username + "    pass:" + u.password);
        is.close();       
    }    
}

该用户是一个实现可序列化的类。

您可以为每个用户对象拥有多个文件,并将其保存在具有不同文件名的目录中。通过使用目录文件名读取器方法,您可以读回该目录中的所有文件名。然后读取每个文件并转换为用户对象。我认为附加一个对象流是行不通的

        ArrayList<User> people = new ArrayList<User>();
    User user;

    User u1 = new User(username, password, "hello", null, null, null);

    people.add(u1);
    System.out.println(people.size());
    for (User people1 : people) {
        System.out.println("user  " + people1.username + "  pass "
                + people1.password);
    }
    for (User p : people) {
        FileOutputStream fout = new FileOutputStream("/tempObjects/"
                + p.getUserName(), true); // assume user name is unique
        ObjectOutputStream out = new ObjectOutputStream(fout);
        out.writeObject(p);
        out.flush();
        out.close();
    }

    System.out.println("success");

    File folder = new File("/tempObjects/"); //reading the folder
    File[] listOfFiles = folder.listFiles(); // all file names in that folder

    for (int i = 0; i < listOfFiles.length; i++) { // reading each file details
        if (listOfFiles[i].isFile()) {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(listOfFiles[i].getName()));
            User u = (User) is.readObject();
            System.out.println("File  user :" + u.username + "    pass:"    + u.password);
            is.close();
        }
    }

您只读取第一个对象…在向其写入对象时不允许追加文件..检查对象文件是否存在的更好方法是,如果存在,则首先读取对象并将其读取到ArrayList,然后将新用户对象添加到该数组,最后将同一数组写入文件…如何读取文件的所有对象?