日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android序列化的存储和读取

發布時間:2025/7/14 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android序列化的存储和读取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? Android中序列化的實現有兩種方式:Serializable接口和Parcelable接口,本文對這兩種方式進行簡單的總結和使用。

一.相關概念

(一)序列化的原因(序列化能實現的效果)

1.永久性保存對象,保存對象的字節序列到本地文件中;

2.對象在網絡中傳遞;

3.對象在IPC間傳遞。

(二)序列化的方法

???????在android系統中關于序列化的方法一般有兩種,分別是實現Serializable接口和Parcelable接口,其中Serializable接口是來自Java中的序列化接口,而Parcelable是Android自帶的序列化 接口。 上述的兩種序列化接口都有各自不同的優缺點,我們在實際使用時需根據不同情況而定。

1.當需要內存較多時使用Parcelable接口。

???????Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC,而相比之下 Parcelable的性能更高(畢竟是Android自帶的),所以當在使用內存時(如:序列化對象在網絡中傳遞對象或序列化在進程間傳遞對象),更推薦使用Parcelable接口。

2.當需要本地存儲時,使用Serializable 接口。

???????但Parcelable有個明顯的缺點:不能能使用在要將數據存儲在磁盤上的情況(如:永久性保 存對象,保存對象的字節序列到本地文件中),因為Parcel本質上為了更好的實現對象在 IPC間傳遞,并不是一個通用的序列化機制,當改變任何Parcel中數據的底層實現都可能導致之前的數據不可讀取,所以此時還是建議使用Serializable 。

二.Serializable接口的使用

???????Serializable的接口實現很簡單,只需讓需要序列化的類繼承Serializable即可,系統會自動將其序列化。存儲時使用FileOutputStream構造一個ObjectOutputStream,使用writeObject 存儲對象。讀取時使用FileInputStream構造一個ObjectInputStream,使用readObject讀取對象。

(一)布局文件activity_main.xml的設計

<LinearLayoutxmlns: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="保存數據" /> <Button android:onClick="read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取數據" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="數據" android:id="@+id/main_tv" /> </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

???????界面設計:通過幾個輸入框輸入數據,兩個按鈕一個保存數據一個讀取數據,讀取的數據顯示在一個文本框下。

(二)創建一個屬性類繼承Serializable

package com.example.lesson18_serializable; import java.io.Serializable; /***屬性類,用來存儲數據,繼承接口Serializable,但是什么方法都不用重寫!*/ public class People implements Serializable{ //定義基本信息 String name; String password; int age; //無參構造方法 public People() { super(); } //有參構造方法,方便數據寫入 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 ; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

(三)主方法的類

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"; //定義布局內的控件 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); } //保存數據 public void save(View view){ ObjectOutputStream fos=null; try { //如果文件不存在就創建文件 File file=new File(path); //file.createNewFile(); //獲取輸出流 //這里如果文件不存在會創建文件,這是寫文件和讀文件不同的地方 fos=new ObjectOutputStream(new FileOutputStream(file)); //獲取輸入框內的文件進行寫入 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) { } } } //讀取數據 public void read(View view){ ObjectInputStream ois=null; try { Log.e("TAG", new File(path).getAbsolutePath()+"<---"); //獲取輸入流 ois=new ObjectInputStream(new FileInputStream(new File(path))); //獲取文件中的數據 Object people=ois.readObject(); //把數據顯示在TextView中 text.setText(people.toString()); } catch (Exception e) { e.printStackTrace(); }finally{ try { if (ois!=null) { ois.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
這里使用但是外部存儲的方式來存儲數據,需要添加權限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • 1
  • 2
  • 1
  • 2

程序運行后的界面:

輸入對應的信息,點擊保存,再點擊讀取顯示的結果:

其中這里的數據是保存再本地文件中的,下次不用寫入數據,可以直接讀取上次寫入的文件。

三.Parcelable接口的使用

???????使用的方法過程要麻煩一些!

實現Parcelable接口主要可以分為一下幾步:

1.讓屬性類Model實現Parcelable接口

2.重寫writeToParcel方法,將你的對象序列化為一個Parcel對象,

即:將類的數據寫入外部提供的Parcel中,打包需要傳遞的數據到Parcel容器保存,以便從Parcel容器獲取數據。 這里的文件的寫入方法非常重要。

3.重寫describeContents方法,內容接口描述,默認返回0即可。 這個方法基本沒有用!

4.實例化靜態內部對象CREATOR實現接口Parcelable.Creator,并重寫讀取的抽象方法。

???????這里的讀取的方法也是很重要的,必須和寫的時候的順序是一致的。這里的CREATOR接口對象的名字是固定的,如果改成其他名字底層會識別不到這個接口!

???????注意:若將Parcel看成是一個流,則先通過writeToParcel把對象寫到流里面,再通過 createFromParcel從流里讀取對象,因此類實現的寫入順序和讀出順序必須一致。

???????這里設計程序從一個頁面跳轉到另一個頁面,并把對象的數據傳遞過去。

(一)設計屬性類繼承Parcelable接口

package com.example.lesson18_parcalable; import android.os.Parcel; import android.os.Parcelable; /***屬性類,繼承Parcelable*實現兩個方法,在其中一個方法內實現對象寫入的操作*創建一個接口類CREATOR,重寫讀取對象的方法*/ public class User implements Parcelable{ //User的各種數據的定義 String name; String password; int age; double money; boolean isAdmin; public User(){} //寫一個構造方法來方便寫入數據 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 // 寫數據的底層實現 public void writeToParcel(Parcel arg0, int arg1) { arg0.writeString(name); arg0.writeString(password); arg0.writeInt(age); arg0.writeDouble(money); //把布爾類型的數據做處理,true1,false0 arg0.writeInt(isAdmin?1:0); } //實例化靜態內部對象CREATOR實現接口,CREATOR名字不能改變,否則會報錯 public static Creator CREATOR=new Creator<User>() { @Override // 讀書數據的底層實現,要和寫入的數據的順序保持一致 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(); //布爾類型的數據要處理 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 + "]"; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

(二)主方法的類的設計

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("跳轉到B頁面"); setContentView(button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //跳轉到另一個頁面,對象的數據也要傳遞過去 Intent intent=new Intent(MainActivity.this,OtherActivity.class); //定義數據 User user=new User("liwenzhi","123456",22,1000000,true); //把數據放到Intent對象里面 intent.putExtra("user", user); //實現頁面跳轉 startActivity(intent); } }); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

???????上面這個類也是很簡單的。設計一個按鈕監聽跳轉到另一個頁面。

(三)另一個頁面的設計

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); //獲取傳遞過來的數據 User user=getIntent().getParcelableExtra("user"); textView.setText(user.toString()); setContentView(textView); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

???????上面的頁面也是比較簡單的,接收從上一個頁面傳遞過來的對象,然后顯示在一個TextView。

程序運行后的顯示界面:

點擊大按鈕后,顯示的界面:

上面的數據的寫死的,其實也是可以向第一個程序那樣使用幾個輸入框來確定數據的。

???????對比這兩個接口實現的方法和效果:
???????對于第一個程序使用Serializable實現了數據的傳遞,并且數據是保存在本地的,即使是程序被卸載了,其他程序只要是文件路徑正確,也可以訪問保存的文件的數據,也是可以用來做進程間的通信的,但是這樣需要消耗一些內存。
???????對比第二個程序使用Parcalable實現了數據的傳遞,這里的數據是不能保存到本地的,占用的內存較少,比較適合用于進程間的數據傳遞。
對于應用方面:網絡信息傳遞和進程間數據傳遞使用Parcalable實現了數據的傳遞的方式是比較多一點的。
???????對于這兩種數據傳遞的信息大小一般不能是很大的數據。

轉載于:https://www.cnblogs.com/Free-Thinker/p/7423910.html

總結

以上是生活随笔為你收集整理的Android序列化的存储和读取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。