java中序列化和反序列化如何操作arraylist_android 如何将ArrayList对象保存到sd卡文件中,需要时在读取处理用--序列化和反序列化的应用...
项目需要将服务器下拉的数据(ArrayList对象类型)保存到本地SD卡中。此时就是java对象序列化和反序列化的应用了。写了一个简单的小例子:先点击序列化按钮,再点击反序列化按钮 ,如果反序列化成功,将显示值出来。public class TestProgress extends Activity {Button but1,but2;TextView tv1,tv2;int count=0;..
项目需要将服务器下拉的数据(ArrayList对象类型)保存到本地SD卡中。此时就是java对象序列化和反序列化的应用了。
写了一个简单的小例子:
先点击序列化按钮,
再点击反序列化按钮 ,如果反序列化成功,将显示值出来。

public class TestProgress extends Activity {
Button but1,but2;
TextView tv1,tv2;
int count=0;
private static String mSDCARD = null;
ArrayList>arrayList;
ArrayList
Object>>arrayList2;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
but1=(Button)findViewById(R.id.but1);
but2=(Button)findViewById(R.id.but2);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
arrayList=new
ArrayList>();
HashMap
Object>map=new HashMap
Object>();
map.put("id", "1");
map.put("name", "xuliehua");
HashMap
Object>map1=new HashMap
Object>();
map1.put("id", "2");
map1.put("name", "fanxuliehua");
arrayList.add(map);
arrayList.add(map1);
String
filenameString=getHcExternalFile("ddddd","/object"+".obj");
final File file =new File(filenameString);
but1.setOnClickListener(new
View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//序列化
try {
FileOutputStream fis=new FileOutputStream(file);
ObjectOutputStream ois=new ObjectOutputStream(fis);
ois.writeObject(arrayList);
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
but2.setOnClickListener(new
View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
FileInputStream fis=new FileInputStream(file);
ObjectInputStream ois =new ObjectInputStream(fis);
arrayList2=(ArrayList
Object>>)= ois.readObject();
ois.close();
fis.close();
if(arrayList2!=null){
tv2.setText(arrayList2.get(0).get("name").toString());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
public static String
getHcExternalFile(String subFolder, String fileName) {
try {
String subFolderPath = getSDCardPath() + "/handcent/"
+ subFolder + "/";
File fi = new File(subFolderPath);
if (!fi.exists()) {
fi.mkdirs();
}
return fi.getAbsolutePath() + fileName;
} catch (Exception exp) {
exp.printStackTrace();
return null;
}
}
public static String
getSDCardPath() {
if (mSDCARD == null) {
mSDCARD =
Environment.getExternalStorageDirectory().toString();
if (mSDCARD == null) {
mSDCARD = "/sdcard";
}
}
return mSDCARD;
}
}
更多推荐



所有评论(0)