Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android利用Serializable保存自定義數據至本地

Android利用Serializable保存自定義數據至本地

編輯:關於Android編程

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 studentsArrayList = new ArrayList();
		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 savedArrayList =(ArrayList) 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如下:




    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved