以前儲存本地我隻會用到SQLite,xml和sp。 今天大佬突然說你不會歸檔嗎?(大佬是ios的),我說什麼是歸檔,結果一查,就是安卓的 序列化serializable
我以前隻會在acitivity跳轉傳對象的時候才會用到serializable,原來serializable還有将對象儲存本地的作用,具體思想就是将對象序列化之後,用file檔案讀寫方式将序列化後的對象儲存到外部存儲,原理上還是用檔案存儲的方式。
用法: 1.首先聲明一個類,這個類和裡面的所有内部類都要implements serializable public class VolunteerListBean implements Serializable{
private List<ConsulterListBean> consulterList ;
public List<ConsulterListBean> getConsulterList() {
return consulterList ;
}
public void setConsulterList(List<ConsulterListBean> consulterList) {
this. consulterList = consulterList ;
}
public static class ConsulterListBean implements Serializable{
private String college ;
private String major ;
private String id ;
public String getCollege() {
return college ;
}
public void setCollege(String college) {
this. college = college ;
}
public String getMajor() {
return major ;
}
public void setMajor(String major) {
this. major = major ;
}
public String getId() {
return id ;
}
public void setId(String id) {
this. id = id ;
}
}
}
假設有一個志願者數組的類,内部又有各個志願者的類,它們都implements serializable
然後在activity中new出志願者數組的對象,各種set get之後就可以調用存儲方法了 //将志願者按照學校名儲存到本地
public void saveVolunteers(VolunteerListBean volunteerListBean , String schoolName) throws IOException , ClassNotFoundException {
String path = Constant. VOLUNTEERS_DIR + File. separator + schoolName + ".data" ; //設定路徑
FileOutputStream fileOutputStream ; //打開檔案輸出流
ObjectOutputStream objectOutputStream ; //打開對象輸出流
File file = new File(path) ; //建立檔案
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs() ;
}
if (file.exists()) {
file.delete() ;
}
file.createNewFile() ;
fileOutputStream = new FileOutputStream(file.toString()) ; //将建立的檔案寫入檔案輸出流
objectOutputStream = new ObjectOutputStream(fileOutputStream) ; //向對象輸出流寫入檔案輸出流
objectOutputStream.writeObject(volunteerListBean) ; //将序列化後的對象寫入對象輸出流
objectOutputStream.close() ; //關閉對象輸出流
fileOutputStream.close() ; //關閉檔案輸出流
}
然後就能在對應的檔案夾裡面看到.data檔案了
接着就是讀檔案了 //讀取志願者,根據學校名
public VolunteerListBean getVolunteers(String schoolName) throws IOException , ClassNotFoundException {
String path = Constant. VOLUNTEERS_DIR + File. separator + schoolName + ".data" ; //設定路徑
VolunteerListBean volunteerListBean ; //聲明對象
File file = new File(path) ; //建立檔案
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs() ;
}
if (file.exists()) {
//如果檔案存在
FileInputStream fileInputStream ; //打開檔案輸入流
ObjectInputStream objectInputStream ; //打開對象輸入流
fileInputStream = new FileInputStream(file.toString()) ; //将建立的檔案寫入檔案輸入流
objectInputStream = new ObjectInputStream(fileInputStream) ; //将檔案輸入流寫入對象輸入流
volunteerListBean = (VolunteerListBean) objectInputStream.readObject() ; //擷取對象輸入流的對象
objectInputStream.close() ; //關閉對象輸入流
fileInputStream.close() ; //關閉檔案輸入流
return volunteerListBean ;
} else {
return null;
}
}
下面還有個小tip,利用遞歸删除指定檔案夾(包括裡面所有檔案) //删除檔案夾
public void deleteDir(File file){
if (file.isFile()) {
//如果是檔案的話
file.delete() ;
return;
}
if (file.isDirectory()) {
//如果是檔案夾的話
File[] childFile = file.listFiles() ; //擷取這個檔案夾裡面的所有檔案(包括檔案夾)
if (childFile == null || childFile. length == 0) {
//如果這個檔案夾沒有子檔案或者檔案夾的話,就删除掉這個檔案(檔案夾)
file.delete() ;
return;
}
for (File f : childFile) {
//遞歸這個方法
deleteDir(f) ;
}
file.delete() ; //這個方法最後就是删除掉目标檔案
}
}