Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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内容的文件仅显示存在1个元素_Java - Fatal编程技术网

Java 反序列化包含arraylist内容的文件仅显示存在1个元素

Java 反序列化包含arraylist内容的文件仅显示存在1个元素,java,Java,因此,我在程序开始时加载该文件,并向其添加任何值,然后保存它。再次加载时,仅显示第一个元素存在(size()返回1) 为什么它不显示新添加的元素 String name; String desig; String age; static ArrayList<Person> personarray = new ArrayList<Person>(); private void SaveButton(java.awt.event.ActionEvent evt) {

因此,我在程序开始时加载该文件,并向其添加任何值,然后保存它。再次加载时,仅显示第一个元素存在(size()返回1) 为什么它不显示新添加的元素

String name;
String desig;
String age;
static ArrayList<Person> personarray = new ArrayList<Person>();

private void SaveButton(java.awt.event.ActionEvent evt) {                                         

        Person newperson = new Person();
        name = jTextField1.getText();
        age = jTextField2.getText();
        desig = jTextField3.getText();


        newperson.saveData(name, age, desig);
        personarray.add(newperson);

        try{
           FileOutputStream fileOut = new FileOutputStream(System.getProperty("user.dir") +"/info.dat", true);
           ObjectOutputStream out = new ObjectOutputStream(fileOut);
           out.writeObject(personarray);
           out.close();
           fileOut.close();
           System.out.print("done");
        }
        catch(IOException i) {
         i.printStackTrace();
      }
    }


    private void LoadButton(java.awt.event.ActionEvent evt) {                                         
        ArrayList<Person> retrievedList = null;
        try {
         FileInputStream fileIn = new FileInputStream(System.getProperty("user.dir") +"/info.dat");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         retrievedList = (ArrayList<Person>) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i) {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c) {
         System.out.println("class not found");
         c.printStackTrace();
         return;
      }
        Person temp = null;
        for(int i = 0; i < retrievedList.size(); i++ ){
            temp = retrievedList.get(i);

            jTable1.getModel().setValueAt(temp.name, i, 0);
            jTable1.getModel().setValueAt(temp.age, i, 1);
            jTable1.getModel().setValueAt(temp.desig, i, 2);
        }
    public static void main(String args[]) {
       personarray = getSavedList(); // gets the file similar to LoadButton
       System.out.print(personarray.size());
        try {

          File file = new File(System.getProperty("user.dir") +"/info.dat");

          if (file.createNewFile()){
            System.out.println("File is created!");
          }else{
            System.out.println("File already exists.");
          }

        } catch (IOException e) {
          e.printStackTrace();
    }
字符串名;
弦设计;
弦年龄;
静态ArrayList personarray=新ArrayList();
私有void保存按钮(java.awt.event.ActionEvent evt){
Person newperson=newperson();
name=jTextField1.getText();
age=jTextField2.getText();
desig=jTextField3.getText();
保存数据(姓名、年龄、设计);
personarray.add(newperson);
试一试{
FileOutputStream fileOut=新的FileOutputStream(System.getProperty(“user.dir”)+“/info.dat”,true);
ObjectOutputStream out=新的ObjectOutputStream(fileOut);
out.writeObject(personarray);
out.close();
fileOut.close();
系统输出打印(“完成”);
}
捕获(IOI异常){
i、 printStackTrace();
}
}
私有void加载按钮(java.awt.event.ActionEvent evt){
ArrayList retrievedList=null;
试一试{
FileInputStream fileIn=newfileinputstream(System.getProperty(“user.dir”)+“/info.dat”);
ObjectInputStream in=新的ObjectInputStream(fileIn);
.readObject()中的retrievedList=(ArrayList);
in.close();
fileIn.close();
}捕获(IOI异常){
i、 printStackTrace();
返回;
}捕获(ClassNotFoundException c){
System.out.println(“未找到类”);
c、 printStackTrace();
返回;
}
人员温度=空;
for(int i=0;i
我怀疑问题在于您正在附加到文件。您使用的是
新文件输出流(System.getProperty(“user.dir”)+“/info.dat”,true)
。第二个参数,
true
,导致在文件末尾写入字节,而不会覆盖现有的文件内容。因此,旧版本的人员列表仍然位于文件的开头,这是从文件读取时读取的


尝试将
false
放在那里。

请缩进您的代码,不要发布不相关的内容(LoadButton和所有AWT内容),并发布明确相关的内容(getSavedList)。创建一个最小的完整示例(不需要AWT内容)复制问题,正确缩进。我为缩进道歉,它在我发布之前看起来很好。getSavedList()是LoadButton()的复制粘贴它只返回retrievedList变量,所以我认为最好发布最少的代码。这就是为什么LoadButton是相关的。我为所有错误道歉,因为我是Java新手。这是全部工作代码。如果您需要帮助,请按照我要求的做。现在是时候尝试修复一个问题,而不能重现它了,甚至查看导致问题的实际代码。尤其是当导致问题所需的完整代码可以发布在30行左右的代码中时。