如何将ArrayList <myobject>保存到文件中?

时间:2016-11-11 17:04:16

标签: java android arraylist fileoutputstream objectoutputstream

如何将ArrayList保存到文件中? 我在做什么 ng?

我使用这个SO问题来帮助我使用Serializable对象。:

how to serialize ArrayList on android

我在如何编写数组列表时使用了这个问题:

Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

然而,当我尝试写入文件时,我收到错误:

  

java.io.NotSerializableException:at

     

中的java.io.ObjectOutputStream.writeObject      

com.mycompany.MyClass.saveData

以下是尝试保存文件的MyClass

private ArrayList < MyCustomObject > arrayList;
private File dataFile;
private String FILE_NAME = "FILE_DATA.dat";

public void init(final Context context) {
    this.appContext = context;

    dataFile = new File(appContext.getFilesDir(), FILE_NAME);

    if (dataFile.exists()) {
        loadData();

    } else {

        try {
            dataFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        arrayList = new ArrayList < MyCustomObject > ();
saveData();
   }

}


private void saveData() {
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(dataFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {

        if (fos != null) {

            ObjectOutputStream oos = null;
            try {

                oos = new ObjectOutputStream(fos);

                if (oos != null) {

                    oos.writeObject(arrayList);

                }
                assert oos != null;
                oos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

private void loadData() {

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(dataFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {

        if (fis != null) {

            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(fis);

                if (ois != null) {

                    try {

                        arrayList = (ArrayList < MyCustomObject > ) ois.readObject();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                } else {

                }
                assert ois != null;
                ois.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

这是MyCustomObject

public class MyCustomObject implements Serializable {

    public String myname = "";
    public String someOtherItem = "";
    public int aNumber = 0;
    public MyCustomObject getCustomObject() {
        return this;
    }
}

1 个答案:

答案 0 :(得分:1)

替换此方法

 public MyCustomObject MyCustomObject() {
        return this;
 }
在你的MyCustomObject类中

,你的代码应该可以正常工作。使用像

这样的东西
 public MyCustomObject getMyCustomObject() {
        return this;
 }

因为当你不自己提供构造函数时,你为方法命名的方式与java为MyCustomObject类创建的默认构造函数相冲突。我假设你使用这种方法能够将一个MyCustomObject实例添加到您的数组列表中:您实际上并不需要这样的方法,但是通过正确的命名,您仍然可以使用它。

您还应该通过调用saveData()方法将样本数据放入ArrayList中,然后再将其保存到磁盘

以下是您的代码的说明。我不确定你的Context对象是什么,但你正在用它来访问文件路径,所以为了让事情继续下去,我只使用了一个特定的文件路径。

public class MyClass {
    private ArrayList < MyCustomObject > arrayList;
    private File dataFile;
    private String FILE_NAME = "FILE_DATA.dat";

    public void init(final Context context) {


        dataFile = new File("C:\\lompo\\file1.txt");

        if (dataFile.exists()) {
            loadData();

        } else {

            try {
                dataFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            arrayList = new ArrayList < MyCustomObject > ();
            MyCustomObject obj1 = new MyCustomObject();
            obj1.aNumber = 125;
            obj1.myname = "HIS NAME";
            arrayList.add(obj1);
            saveData();
       }

    }

    public static void main(String[] args) {
        MyClass myClazz = new MyClass();
        myClazz.init(null);

        System.out.println("Arraylist has " + myClazz.arrayList.size() + " elements");
    }

    private void saveData() {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(dataFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {

            if (fos != null) {

                ObjectOutputStream oos = null;
                try {

                    oos = new ObjectOutputStream(fos);

                    if (oos != null) {

                        oos.writeObject(arrayList);

                    }
                    assert oos != null;
                    oos.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    private void loadData() {

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(dataFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {

            if (fis != null) {

                ObjectInputStream ois = null;
                try {
                    ois = new ObjectInputStream(fis);

                    if (ois != null) {

                        try {

                            arrayList = (ArrayList < MyCustomObject > ) ois.readObject();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    } else {

                    }
                    assert ois != null;
                    ois.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

正如您在代码中看到的那样,main方法的第一次运行会将文件保存在磁盘上,其中arrayList由一个对象填充。第二次运行从文件中读取,然后我打印了之前保存的元素数量和信息:图片说明了结果

enter image description here