序列化反序列化的使用

Java代码:
package com.mobilercn.util;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.content.Context;
import android.os.Environment;/**
* 文件处理工具类
* 
* @author ShawnXiao
* 
*/
public class YTFileHelper {
private Context mContext;
public static YTFileHelper mSelf;
private YTFileHelper() {
} 
public static YTFileHelper getInstance() { 
if (null == mSelf) {
   mSelf = new YTFileHelper();
  }
  return mSelf; } 
public void setContext(Context context) {
  mContext = context;
}
/**
  * 检查是否有SD卡,并有读写权限
  * 
  * @return
  */
public static final boolean checkSDCard() {  
  boolean result = false;
  String state = Environment.getExternalStorageState(); 
if (state.toLowerCase().equals(Environment.MEDIA_MOUNTED.toLowerCase())) {
   result = true;
  }
  return result; 
}
/**
  * 创建一个手机文件
  * 
  * @param filename
  * @return
  */
public File createFile(String filename) {
  File file = null;
  try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    file = new File(rootPath + "/SOP/" + filename);
   }
  } catch (Exception e) {
  }
  return file;
} 
/**
  * 创建一个手机文件
  * 
  * @param filename
  *            文件名
  * @return 布尔值
  */
public boolean isFileExist(String filename) {
  File file = null;
  try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    file = new File(rootPath + "/SOP/" + filename);
    // if (D2EConfigures.TEST) {
    // Log.e("fileExist----------->", file.exists() ? "存在" : "不存在");
    // }
    if (file.exists()) {     return true;
    }
   }
  } catch (Exception e) {
  }
  return false;
} /**
  * 删除一个文件
  * 
  * @param filename
  *            文件名
  */
public void deletExistFile(String filename) {
  File file = null;
  try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    file = new File(rootPath + "/SOP/" + filename);
    if (file.exists()) {
     file.delete();
     // boolean isSuccess = file.delete();
     // if (D2EConfigures.TEST) {
     // Log.e("删除文件是否成功:", isSuccess ? "是" : "否");
     // }
    }
   }
  } catch (Exception e) {
  }
} /**
  * 读取文件中的数据
  * 
  * @param filename
  *            文件名
  * @return 字节数组数据
  */
public final byte[] readFile(String filename) {
  FileInputStream fis = null;
  byte[] bytes = null;
  try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    File file = new File(rootPath + "/SOP/" + filename);
    fis = new FileInputStream(file);
    bytes = JJNetHelper.readByByte(fis, -1);
   } else {
    fis = mContext.openFileInput(filename);
    bytes = JJNetHelper.readByByte(fis, -1);
   }
  } catch (Exception e) {
   bytes = null;
  } finally {
   try {
    if (fis != null) {
     fis.close();
     fis = null;
    }
   } catch (Exception e) {
   }
  }  return bytes;
}
/**
  * 保存文件
  * 
  * @param filename
  *            要保存数据为文件的文件名
  * @param data
  *            要保存的字节数组数据
  */
public final void saveFile(String filename, byte[] data) { 
FileOutputStream fos = null;  
try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    File file = new File(rootPath + "/SOP");
    if (!file.exists()) {
     file.mkdir();
    }
    File png = new File(file.getPath() + "/" + filename);
    if (png.exists()) {
     png.delete();
    }
    fos = new FileOutputStream(png, true);
    fos.write(data);
    fos.close();
   } else {
    fos = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
    fos.write(data);
    fos.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (fos != null) {
    try {
     fos.close();
     fos = null;
    } catch (Exception e) {
    }
   }
  } 
} 
/**
  * 将对象数据序列化为文件
  * 
  * @param filename
  *            要序列化为文件的文件名
  * @param data
  *            对象数据
  */
public final void serialObject(String filename, Object data) {
  // if (D2EConfigures.TEST) {
  // Log.e("data----------->", "" + data);
  // }  FileOutputStream fos = null;
  ObjectOutputStream oos = null;  
try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    File file = new File(rootPath + "/SOP");
    if (!file.exists()) {
     file.mkdir();
    }
    File png = new File(file.getPath() + "/" + filename);
    if (png.exists()) {
     png.delete();
    }
    fos = new FileOutputStream(png, true);
    oos = new ObjectOutputStream(fos);
    oos.writeObject(data);
    // if (D2EConfigures.TEST) {
    // Log.e("xxxxxxxxxxx", "" + (oos.toString()));
    // }
    oos.close();
    fos.close();
   } else {
    fos = mContext.openFileOutput(filename, Context.MODE_APPEND);
    oos = new ObjectOutputStream(fos);
    oos.writeObject(data);
    // if (D2EConfigures.TEST) {
    // Log.e("xxxxxxxxxxx", "" + (oos.toString()));
    // }
    oos.close();
    fos.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (fos != null || oos != null) {
    try {
     oos.close();
     oos = null;
     fos.close();
     fos = null;
    } catch (Exception e) {
    }
   }
  }
}
/**
  * 反序列化文件为对象
  * 
  * @param filename
  *            保存的文件名
  * @return 对象数据
  */
public final Object deSerialObject(String filename) {
  FileInputStream fis = null;
  ObjectInputStream ois = null;
  Object data = null;
  try {
   if (checkSDCard()) {
    File rootDir = Environment.getExternalStorageDirectory();
    String rootPath = rootDir.getAbsolutePath();
    File file = new File(rootPath + "/SOP/" + filename);
    fis = new FileInputStream(file);
    ois = new ObjectInputStream(fis);
    data = ois.readObject();
    ois.close();
    fis.close();
   } else {
    fis = mContext.openFileInput(filename);
    ois = new ObjectInputStream(fis);
    data = ois.readObject();
    ois.close();
    fis.close();
   }
  } catch (Exception e) {
   data = null;
  } finally {
   try {
    if (fis != null || ois != null) {
     ois.close();
     ois = null;
     fis.close();
     fis = null;
    }
   } catch (Exception e) {
   }
  }  return data;
}
}



//一个工具类,使用的时候 
//序列化:
YTFileHelper  ytfileHelper=  YTFileHelper.getInstance();
ytfileHelper.setContext(Context mContext);//设置自己的上下文
Object data=new Object();//这是自己要序列化的的对象
String str_my_data_name="datafile";//这是自己定义的文件名字
ytfileHelper.serialObject(str_my_data_name,data);//序列化该对象
//这样就把对象保存到文件中了,主要还是为了使用保存的对象,通过反序列化得到保存的对象,然后使用


//反序列化:
Object myData=null;
YTFileHelper  ytfileHelper=  YTFileHelper.getInstance();
ytfileHelper.setContext(Context mContext);//设置自己的上下文
String str_my_data_name="datafile";
myData=(Object)ytfileHelper.deSerialObject(str_my_data_name);
//然后在这里对得到的对象,处理该对象,添加信息,修改信息等
//如果还要保存信息,则继续序列化
-------------使用过程中的注意点--------------
1、由于每次序列化都有判断原来的文件是否存在,存在则删除,从新保存文件。
     所以,最好是先反序列化获取保存的对象,然后在修改对象中的属性,然后在序列化保存。
2、最后可能需要释放文件,因为用不到了,具体如:采集到的数据成功提交到服务器了,就释放掉
    调用如下方法:
YTFileHelper  ytfileHelper=  YTFileHelper.getInstance();
ytfileHelper.setContext(Context mContext);//设置自己的上下文
ytfileHelper.deletExistFile(String fileName);
File的delete()方法,删除成功不一定返回为真,如果文件夹下面还包含其他文件就返回假

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值