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