Android 序列化的存儲和讀取總結(jié)及簡單使用
Android 序列化
1.序列化的目的
(1).永久的保存對象數(shù)據(jù)(將對象數(shù)據(jù)保存在文件當(dāng)中,或者是磁盤中
(2).通過序列化操作將對象數(shù)據(jù)在網(wǎng)絡(luò)上進行傳輸(由于網(wǎng)絡(luò)傳輸是以字節(jié)流的方式對數(shù)據(jù)進行傳輸?shù)?因此序列化的目的是將對象數(shù)據(jù)轉(zhuǎn)換成字節(jié)流的形式)
(3).將對象數(shù)據(jù)在進程之間進行傳遞(Activity之間傳遞對象數(shù)據(jù)時,需要在當(dāng)前的Activity中對對象數(shù)據(jù)進行序列化操作.在另一個Activity中需要進行反序列化操作講數(shù)據(jù)取出)
(4).Java平臺允許我們在內(nèi)存中創(chuàng)建可復(fù)用的Java對象,但一般情況下,只有當(dāng)JVM處于運行時,這些對象才可能存在,即,這些對象的生命周期不會比JVM的生命周期更長(即每個對象都在JVM中)但在現(xiàn)實應(yīng)用中,就可能要停止JVM運行,但有要保存某些指定的對象,并在將來重新讀取被保存的對象。這是Java對象序列化就能夠?qū)崿F(xiàn)該功能。(可選擇入數(shù)據(jù)庫、或文件的形式保存)
(5).序列化對象的時候只是針對變量進行序列化,不針對方法進行序列化.
(6).在Intent之間,基本的數(shù)據(jù)類型直接進行相關(guān)傳遞即可,但是一旦數(shù)據(jù)類型比較復(fù)雜的時候,就需要進行序列化操作了.
Android中序列化的實現(xiàn)有兩種方式:Serializable接口和Parcelable接口,本文對這兩種方式進行簡單的總結(jié)和使用。
一.相關(guān)概念
(一)序列化的原因(序列化能實現(xiàn)的效果)
1.永久性保存對象,保存對象的字節(jié)序列到本地文件中;
2.對象在網(wǎng)絡(luò)中傳遞;3.對象在IPC間傳遞。
(二)序列化的方法
在Android系統(tǒng)中關(guān)于序列化的方法一般有兩種,分別是實現(xiàn)Serializable接口和Parcelable接口,其中Serializable接口是來自Java中的序列化接口,而Parcelable是Android自帶的序列化 接口。 上述的兩種序列化接口都有各自不同的優(yōu)缺點,我們在實際使用時需根據(jù)不同情況而定。
1.當(dāng)需要內(nèi)存較多時使用Parcelable接口。
Serializable在序列化的時候會產(chǎn)生大量的臨時變量,從而引起頻繁的GC,而相比之下 Parcelable的性能更高(畢竟是Android自帶的),所以當(dāng)在使用內(nèi)存時(如:序列化對象在網(wǎng)絡(luò)中傳遞對象或序列化在進程間傳遞對象),更推薦使用Parcelable接口。
2.當(dāng)需要本地存儲時,使用Serializable 接口。
但Parcelable有個明顯的缺點:不能能使用在要將數(shù)據(jù)存儲在磁盤上的情況(如:永久性保 存對象,保存對象的字節(jié)序列到本地文件中),因為Parcel本質(zhì)上為了更好的實現(xiàn)對象在 IPC間傳遞,并不是一個通用的序列化機制,當(dāng)改變?nèi)魏蜳arcel中數(shù)據(jù)的底層實現(xiàn)都可能導(dǎo)致之前的數(shù)據(jù)不可讀取,所以此時還是建議使用Serializable 。
二.Serializable接口的使用
Serializable的接口實現(xiàn)很簡單,只需讓需要序列化的類繼承Serializable即可,系統(tǒng)會自動將其序列化。存儲時使用FileOutputStream構(gòu)造一個ObjectOutputStream,使用writeObject 存儲對象。讀取時使用FileInputStream構(gòu)造一個ObjectInputStream,使用readObject讀取對象。
(一)布局文件activity_main.xml的設(shè)計
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_et_name" android:hint="你的用戶名" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_et_password" android:hint="你的密碼" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_et_age" android:hint="你的年齡" /> <Button android:onClick="save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存數(shù)據(jù)" /> <Button android:onClick="read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取數(shù)據(jù)" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="數(shù)據(jù)" android:id="@+id/main_tv" /> </LinearLayout>
界面設(shè)計:通過幾個輸入框輸入數(shù)據(jù),兩個按鈕一個保存數(shù)據(jù)一個讀取數(shù)據(jù),讀取的數(shù)據(jù)顯示在一個文本框下。
(二)創(chuàng)建一個屬性類繼承Serializable
package com.example.lesson18_serializable; import java.io.Serializable; /** *屬性類,用來存儲數(shù)據(jù),繼承接口Serializable,但是什么方法都不用重寫! */ public class People implements Serializable{ //定義基本信息 String name; String password; int age; //無參構(gòu)造方法 public People() { super(); } //有參構(gòu)造方法,方便數(shù)據(jù)寫入 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"; //定義布局內(nèi)的控件 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); } //保存數(shù)據(jù) public void save(View view){ ObjectOutputStream fos=null; try { //如果文件不存在就創(chuàng)建文件 File file=new File(path); //file.createNewFile(); //獲取輸出流 //這里如果文件不存在會創(chuàng)建文件,這是寫文件和讀文件不同的地方 fos=new ObjectOutputStream(new FileOutputStream(file)); //獲取輸入框內(nèi)的文件進行寫入 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) { } } } //讀取數(shù)據(jù) public void read(View view){ ObjectInputStream ois=null; try { Log.e("TAG", new File(path).getAbsolutePath()+"<---"); //獲取輸入流 ois=new ObjectInputStream(new FileInputStream(new File(path))); //獲取文件中的數(shù)據(jù) Object people=ois.readObject(); //把數(shù)據(jù)顯示在TextView中 text.setText(people.toString()); } catch (Exception e) { e.printStackTrace(); }finally{ try { if (ois!=null) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
這里使用但是外部存儲的方式來存儲數(shù)據(jù),需要添加權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
程序運行后的界面:
輸入對應(yīng)的信息,點擊保存,再點擊讀取顯示的結(jié)果:
其中這里的數(shù)據(jù)是保存再本地文件中的,下次不用寫入數(shù)據(jù),可以直接讀取上次寫入的文件。
三.Parcelable接口的使用
使用的方法過程要麻煩一些!
實現(xiàn)Parcelable接口主要可以分為一下幾步:
1.讓屬性類Model實現(xiàn)Parcelable接口2.重寫writeToParcel方法,將你的對象序列化為一個Parcel對象,
即:將類的數(shù)據(jù)寫入外部提供的Parcel中,打包需要傳遞的數(shù)據(jù)到Parcel容器保存,以便從Parcel容器獲取數(shù)據(jù)。 這里的文件的寫入方法非常重要。
3.重寫describeContents方法,內(nèi)容接口描述,默認(rèn)返回0即可。 這個方法基本沒有用!4.實例化靜態(tài)內(nèi)部對象CREATOR實現(xiàn)接口Parcelable.Creator,并重寫讀取的抽象方法。
這里的讀取的方法也是很重要的,必須和寫的時候的順序是一致的。這里的CREATOR接口對象的名字是固定的,如果改成其他名字底層會識別不到這個接口!
注意:若將Parcel看成是一個流,則先通過writeToParcel把對象寫到流里面,再通過 createFromParcel從流里讀取對象,因此類實現(xiàn)的寫入順序和讀出順序必須一致。
這里設(shè)計程序從一個頁面跳轉(zhuǎn)到另一個頁面,并把對象的數(shù)據(jù)傳遞過去。
(一)設(shè)計屬性類繼承Parcelable接口
package com.example.lesson18_parcalable; import android.os.Parcel; import android.os.Parcelable; /** *屬性類,繼承Parcelable *實現(xiàn)兩個方法,在其中一個方法內(nèi)實現(xiàn)對象寫入的操作 *創(chuàng)建一個接口類CREATOR,重寫讀取對象的方法 */ public class User implements Parcelable{ //User的各種數(shù)據(jù)的定義 String name; String password; int age; double money; boolean isAdmin; public User(){} //寫一個構(gòu)造方法來方便寫入數(shù)據(jù) public User(String name, String password, int age, double money, boolean isAdmin) { super(); this.name = name; this.password = password; this.age = age; this.money = money; this.isAdmin = isAdmin; } @Override // 這個方法沒什么用 public int describeContents() { return 0; } @Override // 寫數(shù)據(jù)的底層實現(xiàn) public void writeToParcel(Parcel arg0, int arg1) { arg0.writeString(name); arg0.writeString(password); arg0.writeInt(age); arg0.writeDouble(money); //把布爾類型的數(shù)據(jù)做處理,true1,false0 arg0.writeInt(isAdmin?1:0); } //實例化靜態(tài)內(nèi)部對象CREATOR實現(xiàn)接口,CREATOR名字不能改變,否則會報錯 public static Creator CREATOR=new Creator<User>() { @Override // 讀書數(shù)據(jù)的底層實現(xiàn),要和寫入的數(shù)據(jù)的順序保持一致 public User createFromParcel(Parcel arg0) { User user=new User(); user.name=arg0.readString(); user.password=arg0.readString(); user.age=arg0.readInt(); user.money=arg0.readDouble(); //布爾類型的數(shù)據(jù)要處理 user.isAdmin=arg0.readInt()==1?true:false; return user; } @Override public User[] newArray(int arg0) { //返回 return new User[arg0]; } }; //從toString方法 @Override public String toString() { return "User [name=" + name + ", password=" + password + ", age=" + age + ", money=" + money + ", isAdmin=" + isAdmin + "]"; } }
(二)主方法的類的設(shè)計
package com.example.lesson18_parcalable; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button button=new Button(this); button.setText("跳轉(zhuǎn)到B頁面"); setContentView(button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //跳轉(zhuǎn)到另一個頁面,對象的數(shù)據(jù)也要傳遞過去 Intent intent=new Intent(MainActivity.this,OtherActivity.class); //定義數(shù)據(jù) User user=new User("liwenzhi","123456",22,1000000,true); //把數(shù)據(jù)放到Intent對象里面 intent.putExtra("user", user); //實現(xiàn)頁面跳轉(zhuǎn) startActivity(intent); } }); } }
上面這個類也是很簡單的。設(shè)計一個按鈕監(jiān)聽跳轉(zhuǎn)到另一個頁面。
(三)另一個頁面的設(shè)計
package com.example.lesson18_parcalable; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView=new TextView(this); textView.setTextSize(30); //獲取傳遞過來的數(shù)據(jù) User user=getIntent().getParcelableExtra("user"); textView.setText(user.toString()); setContentView(textView); } }
上面的頁面也是比較簡單的,接收從上一個頁面?zhèn)鬟f過來的對象,然后顯示在一個TextView。
程序運行后的顯示界面:
點擊大按鈕后,顯示的界面:
上面的數(shù)據(jù)的寫死的,其實也是可以向第一個程序那樣使用幾個輸入框來確定數(shù)據(jù)的。
對比這兩個接口實現(xiàn)的方法和效果:
對于第一個程序使用Serializable實現(xiàn)了數(shù)據(jù)的傳遞,并且數(shù)據(jù)是保存在本地的,即使是程序被卸載了,其他程序只要是文件路徑正確,也可以訪問保存的文件的數(shù)據(jù),也是可以用來做進程間的通信的,但是這樣需要消耗一些內(nèi)存。
對比第二個程序使用Parcalable實現(xiàn)了數(shù)據(jù)的傳遞,這里的數(shù)據(jù)是不能保存到本地的,占用的內(nèi)存較少,比較適合用于進程間的數(shù)據(jù)傳遞。
對于應(yīng)用方面:網(wǎng)絡(luò)信息傳遞和進程間數(shù)據(jù)傳遞使用Parcalable實現(xiàn)了數(shù)據(jù)的傳遞的方式是比較多一點的。
對于這兩種數(shù)據(jù)傳遞的信息大小一般不能是很大的數(shù)據(jù)。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android中AutoCompleteTextView自動提示
這篇文章主要為大家詳細(xì)介紹了Android中AutoCompleteTextView自動提示的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android UI控件之Gallery實現(xiàn)拖動式圖片瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之Gallery實現(xiàn)拖動式圖片瀏覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Flutter?Widget之FutureBuilder使用示例詳解
這篇文章主要為大家介紹了Flutter?Widget之FutureBuilder使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Android實現(xiàn)加載時提示“正在加載,請稍后”的方法
在現(xiàn)在的很多應(yīng)用中,當(dāng)在加載的時候,如果頁面動態(tài)數(shù)據(jù)較多,會有很長一段時間的空白頁面,如果加上這個頁面正在加載的提示,使得應(yīng)用更加人性化。這篇文章就給大家分享了在 Android實現(xiàn)加載時提示“正在加載,請稍后”的方法,有需要的朋友們可以參考借鑒。2016-10-10Android入門之Activity四種啟動模式(standard、singleTop、singleTask、singl
當(dāng)應(yīng)用運行起來后就會開啟一條線程,線程中會運行一個任務(wù)棧,當(dāng)Activity實例創(chuàng)建后就會放入任務(wù)棧中。Activity啟動模式的設(shè)置在AndroidManifest.xml文件中,通過配置Activity的屬性android:launchMode=""設(shè)置2015-12-12Android實現(xiàn)打開各種文件的intent方法小結(jié)
這篇文章主要介紹了Android實現(xiàn)打開各種文件的intent方法,結(jié)合實例形式總結(jié)分析了Android針對HTML、圖片文件、pdf文件、文本文件、音頻文件、視頻文件等的intent打開方法,需要的朋友可以參考下2016-08-08Android Studio實現(xiàn)格式化XML代碼順序
這篇文章主要介紹了Android Studio實現(xiàn)格式化XML代碼順序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android如何自定義EditText光標(biāo)與下劃線顏色詳解
在android開發(fā)中 EditTextText是我們經(jīng)常用到的,我們使用時會有一些小問題,下面這篇文章主要給大家介紹了關(guān)于利用Android如何自定義EditText光標(biāo)與下劃線顏色的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08