MainActivity如下:
package cc.test.serializable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.os.Environment; /** * Demo描述: * 将ArrayList<自定义数据>在SDCard上进行存取. * * Parcelable和Serializable的区别: * 内存间数据传输时推荐使用Parcelable,如activity间传输数据 * 比如:http://blog.csdn.net/lfdfhl/article/details/10961459 * 保存到本地或者网络传输时推荐使用Serializable */ public class TestSerializableActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); testSerializable(); } private void testSerializable() { FileOutputStream fileOutputStream=null; ObjectOutputStream objectOutputStream =null; FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; ArrayList<Student> studentsArrayList = new ArrayList<Student>(); Student student = null; for (int i = 1; i < 5; i++) { student = new Student(i, "小明" + i); studentsArrayList.add(student); } try { //存入数据 File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator +"Test"+File.separator + "data.dat"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } if (!file.exists()) { file.createNewFile(); } fileOutputStream= new FileOutputStream(file.toString()); objectOutputStream= new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(studentsArrayList); //取出数据 fileInputStream = new FileInputStream(file.toString()); objectInputStream = new ObjectInputStream(fileInputStream); ArrayList<Student> savedArrayList =(ArrayList<Student>) objectInputStream.readObject(); for (int i = 0; i < savedArrayList.size(); i++) { System.out.println("取出的数据:" + savedArrayList.get(i).toString()); } } catch (Exception e) { // TODO: handle exception }finally{ if (objectOutputStream!=null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream!=null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (objectInputStream!=null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream!=null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Student如下:
package cc.test.serializable; import java.io.Serializable; public class Student implements Serializable { private Integer id; private String name; //注意定义此字段 public static final long serialVersionUID = 9527L; public Student() { super(); } public Student(Integer id, String name) { super(); this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>