Android中序列化的实现有两种方式:Serializable接口和Parcelable接口,本文对这两种方式进行简单的总结和使用。
在android系统中关于序列化的方法一般有两种,分别是实现Serializable接口和Parcelable接口,其中Serializable接口是来自Java中的序列化接口,而Parcelable是Android自带的序列化 接口。 上述的两种序列化接口都有各自不同的优缺点,我们在实际使用时需根据不同情况而定。
Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC,而相比之下 Parcelable的性能更高(毕竟是Android自带的),所以当在使用内存时(如:序列化对象在网络中传递对象或序列化在进程间传递对象),更推荐使用Parcelable接口。
但Parcelable有个明显的缺点:不能能使用在要将数据存储在磁盘上的情况(如:永久性保 存对象,保存对象的字节序列到本地文件中),因为Parcel本质上为了更好的实现对象在 IPC间传递,并不是一个通用的序列化机制,当改变任何Parcel中数据的底层实现都可能导致之前的数据不可读取,所以此时还是建议使用Serializable 。
Serializable的接口实现很简单,只需让需要序列化的类继承Serializable即可,系统会自动将其序列化。存储时使用FileOutputStream构造一个ObjectOutputStream,使用writeObject 存储对象。读取时使用FileInputStream构造一个ObjectInputStream,使用readObject读取对象。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
界面设计:通过几个输入框输入数据,两个按钮一个保存数据一个读取数据,读取的数据显示在一个文本框下。
package com.example.lesson18_serializable;
import java.io.Serializable;
/**
*属性类,用来存储数据,继承接口Serializable,但是什么方法都不用重写!
*/
public class People implements Serializable{
//定义基本信息
String name;
String password;
int age;
//无参构造方法
public People() {
super();
}
//有参构造方法,方便数据写入
public People(String name, String password, int age) {
super();
this.name = name;
this.password = password;
this.age = age;
}
//重写toString方法,方便显示
@Override
public String toString() {
return "People [name=" + name + ", password=" + password + ", age="
+ age ;
}
}
package com.example.lesson18_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 android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
//保存文件的路径
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/people.txt";
//定义布局内的控件
EditText edit_name;
EditText edit_password;
EditText edit_age;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化布局控件
edit_name=(EditText) findViewById(R.id.main_et_name);
edit_password=(EditText) findViewById(R.id.main_et_password);
edit_age=(EditText) findViewById(R.id.main_et_age);
text=(TextView) findViewById(R.id.main_tv);
}
//保存数据
public void save(View view){
ObjectOutputStream fos=null;
try {
//如果文件不存在就创建文件
File file=new File(path);
//file.createNewFile();
//获取输出流
//这里如果文件不存在会创建文件,这是写文件和读文件不同的地方
fos=new ObjectOutputStream(new FileOutputStream(file));
//获取输入框内的文件进行写入
String name=edit_name.getText().toString();
String password=edit_password.getText().toString();
int age=Integer.parseInt(edit_age.getText().toString());
People people=new People(name, password, age);
//这里不能再用普通的write的方法了
//要使用writeObject
fos.writeObject(people);;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (fos!=null) {
fos.close();
}
} catch (IOException e) {
}
}
}
//读取数据
public void read(View view){
ObjectInputStream ois=null;
try {
Log.e("TAG", new File(path).getAbsolutePath()+"<---");
//获取输入流
ois=new ObjectInputStream(new FileInputStream(new File(path)));
//获取文件中的数据
Object people=ois.readObject();
//把数据显示在TextView中
text.setText(people.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (ois!=null) {
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这里使用但是外部存储的方式来存储数据,需要添加权限:
程序运行后的界面:
输入对应的信息,点击保存,再点击读取显示的结果:
留言与评论(共有 0 条评论) “” |