??????????? Android Debug??????????
Topic?
發(fā)布安卓學(xué)習(xí)過(guò)程中遇到問(wèn)題解決過(guò)程,希望我的解決方案可以對(duì)小伙伴們有幫助。
??筆記目錄
??文件存儲(chǔ)
??內(nèi)部存儲(chǔ)
??存儲(chǔ)數(shù)據(jù)到文件
??從文件中讀取數(shù)據(jù)
??實(shí)戰(zhàn)演練--保存QQ賬號(hào)與密碼
??acticity_main.xml布局文件?
???FileSaveQQ.java文件
??MainActivity.java文件
?驗(yàn)證文件存儲(chǔ)
???結(jié)尾
??文件存儲(chǔ)
文件存儲(chǔ)是Android中最基本的一種數(shù)據(jù)存儲(chǔ)方式,其與Java中的文件存儲(chǔ)類(lèi)似,都是通過(guò)I/O流的形式把數(shù)據(jù)直接存儲(chǔ)到文件中。
如果想要將數(shù)據(jù)存入文件中,有兩種存儲(chǔ)方式,一種是內(nèi)部存儲(chǔ),一種是外部存儲(chǔ)。其中內(nèi)部存儲(chǔ)是將數(shù)據(jù)以文件的形式存儲(chǔ)到應(yīng)用中,外部存儲(chǔ)是將數(shù)據(jù)文件的形式存儲(chǔ)到一些外部設(shè)備上,如SD卡。
今天學(xué)習(xí)的是Android 文件存儲(chǔ)中的內(nèi)部存儲(chǔ)形式。
??內(nèi)部存儲(chǔ)
內(nèi)部存儲(chǔ)是指將應(yīng)用程序中的數(shù)據(jù)與文件的形式存儲(chǔ)到應(yīng)用中,此時(shí)存儲(chǔ)的文件會(huì)被其所在的應(yīng)用存序私有化,如果其他應(yīng)用程序想要操作文應(yīng)用程序中的文件則需要設(shè)置權(quán)限,當(dāng)創(chuàng)建的應(yīng)用程序被卸載時(shí),其內(nèi)部存儲(chǔ)文件也隨之被刪除。
Android開(kāi)發(fā)中,內(nèi)部存儲(chǔ)使用的是 Context 提供的 openFileOutput() 方法和 openFileInput() 方法,這兩種方法能夠返回進(jìn)行讀寫(xiě)操作的 FileoutputStream 對(duì)象和 FileInputstream 對(duì)象。
FileOutputStream fos = openFileOutput(String name,int mode);
FileInputStream fis = openFileInput(String name);
openFileOutput()方法,用打開(kāi)應(yīng)用程序中對(duì)應(yīng)的輸出流,將數(shù)據(jù)存儲(chǔ)到指定的文件中。
openFileInput()方法用于打開(kāi)應(yīng)用程序?qū)?yīng)的輸入流,讀取指定文件中的數(shù)據(jù)。
它們的參數(shù)"name"表示文件名,"mode"表示文件的操作模式,也就是讀寫(xiě)文件的形式.
"mode"的取值有四種,具體如下:
MODE_PRIVATE:?該文件只能被當(dāng)前程序讀寫(xiě);
MODE_APPEND:?該文件的內(nèi)容可以追加;
MODE_WORLD_READABLE:?該文件的內(nèi)容以被其他程序讀;
MODE_WORLD_WRITEABLE:?該文件的內(nèi)容可以被其他程序?qū)憽?/p>
值得注意的是,安卓系統(tǒng)有一套自己的安全模型,默認(rèn)情況下,任何應(yīng)用創(chuàng)建的文件都是私有的,其他程序無(wú)法訪問(wèn),除非在文件創(chuàng)建時(shí)指定的操作模式為MODE_WORLD_READABLE或MODE_WORLD_WRITEABLE,如果希望文件能夠被其他程序進(jìn)行讀寫(xiě)操作,則需要同時(shí)指定該文件MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE的權(quán)限。
??存儲(chǔ)數(shù)據(jù)到文件
存儲(chǔ)數(shù)據(jù)時(shí),使用FileOutputStream對(duì)象將數(shù)據(jù)存儲(chǔ)到文件中,實(shí)例代碼如下:
String fileName = "data.txt"; //文件名稱(chēng)
String content = "helloworld"; //保存數(shù)據(jù)
FileOutputStream fos = null;
try {
fos = context.openFileOutput(FileName,MODE_PRIVATE);
fos.write((content.getBytes()); //將數(shù)據(jù)寫(xiě)入文件中
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
上述代碼中首先定義了兩個(gè)String類(lèi)型的變量fileName和content,這兩個(gè)變量的值”data.txt“ 與 ”helloworld“分別表示文件名與要寫(xiě)入文件的數(shù)據(jù),接著創(chuàng)建了FileOutputStream對(duì)象,fos通過(guò)該對(duì)象的write()方法將數(shù)據(jù)"helloworld"寫(xiě)入"data.txt"文件。
??從文件中讀取數(shù)據(jù)
存儲(chǔ)好數(shù)據(jù)之后,如果需要獲取這些數(shù)據(jù),則需要從文件中讀取存儲(chǔ)的數(shù)據(jù),關(guān)于讀取內(nèi)部存儲(chǔ)文件中的數(shù)據(jù),具體方式如下所示:
String content = "";
FileInputStream fis = null;
try {
fis = context.openFileInput("data.txt"); //獲取文件輸入流對(duì)象
byte[] buffer = new byte[fis.available()]; //創(chuàng)建緩沖區(qū),并獲取文件長(zhǎng)度
fis.read(buffer); //將文件內(nèi)容讀取到buffer緩沖區(qū)
content = new String(buffer); //轉(zhuǎn)換成字符串
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fis != null){
fis.close(); //關(guān)閉輸入流
}
} catch (IOException e) {
e.printStackTrace();
}
}
上述代碼中首先通過(guò)openFileinput()方法獲取到文件輸入流對(duì)象,然后通過(guò)available()方法獲取文件的長(zhǎng)度,并創(chuàng)建相應(yīng)大小的byte數(shù)組作為緩沖區(qū),再通過(guò)read()方法將文件內(nèi)容讀取到buffer緩沖區(qū)中,最后將讀取到的內(nèi)容轉(zhuǎn)換成指定字符串。
??實(shí)戰(zhàn)演練--保存QQ賬號(hào)與密碼
效果演示?
![[Android Studio]Android 數(shù)據(jù)存儲(chǔ)-文件存儲(chǔ)學(xué)習(xí)筆記-結(jié)合保存QQ賬戶(hù)與密碼存儲(chǔ)到指定文件中的演練](https://imgs.yssmx.com/Uploads/2023/04/412778-2.png)
1,創(chuàng)建程序
創(chuàng)建一個(gè)名為SaveQQ的應(yīng)用程序,指定報(bào)名為cn.example.saveqq。?
2,導(dǎo)入界面圖片
將保存QQ密碼界面所需要的圖片head.png導(dǎo)入到項(xiàng)目中的drawable文件夾中,
3,放置界面圖片
在activity_main.xml布局文件中放置一個(gè)ImageView控件,用于顯示用戶(hù)頭像,兩個(gè)TextView控件,用于分別用于顯示"賬號(hào): "與"密碼: "文本信息,兩個(gè)EditText控件分別用于輸入賬號(hào)和密碼信息,一個(gè)Button控件用于顯示登錄按鈕。
4,創(chuàng)建工具類(lèi)
由于QQ賬號(hào)和密碼需要存放在文件中,因此,需要在程序中的cn.example.saveqq包中創(chuàng)建一個(gè)工具類(lèi)FileSaveQQ,在該類(lèi)中實(shí)現(xiàn)QQ賬號(hào)和密碼的存儲(chǔ)與讀取功能。
??acticity_main.xml布局文件?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#E6E6E6">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/head"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="賬號(hào):"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:hint="輸入的是數(shù)字"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_account"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_password"
android:padding="10dp"
android:text="密碼:"
android:textSize="20sp"
android:textColor="#000"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:layout_marginLeft="5dp"
android:background="@null"
android:inputType="textPassword"
android:padding="10dp"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_login"
android:text="登錄"
android:layout_marginTop="25dp"
android:background="#3c8dc4"
android:textColor="@android:color/white"
android:textSize="20sp"/>
</LinearLayout>
???FileSaveQQ.java文件
package com.example.saveqq;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class FileSaveQQ {
public static boolean saveUserinfo(Context context,String account,String password){
FileOutputStream fos = null;
try {
fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
fos.write((account + ":" +password).getBytes());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
try {
if (fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Map<String,String> getUserInfo(Context context){
String content = "";
FileInputStream fis = null;
try {
fis = context.openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
Map<String,String> userMap = new HashMap<String, String>();
String[] infos = content.split(":");
userMap.put("account",infos[0]);
userMap.put("password",infos[1]);
return userMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
if (fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
??MainActivity.java文件
package com.example.saveqq;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Map<String,String> userInfo = FileSaveQQ.getUserInfo(this);
if (userInfo != null){
et_account.setText(userInfo.get("account"));
et_password.setText(userInfo.get("password"));
}
}
private void initView() {
et_account = findViewById(R.id.et_account);
et_password = findViewById(R.id.et_password);
btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_login:
String account = et_account.getText().toString().trim();
String password = et_password.getText().toString();
if (TextUtils.isEmpty(account)){
Toast.makeText(this,"請(qǐng)輸入QQ號(hào)",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this,"請(qǐng)輸入密碼",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this,"登錄成功",Toast.LENGTH_SHORT).show();
boolean isSaveSucess = FileSaveQQ.saveUserinfo(this,account,password);
if (isSaveSucess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"保存失敗",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
?驗(yàn)證文件存儲(chǔ)
為了驗(yàn)證程序是否操作成功,可以通Device File Explorer視圖中找到data/data目錄,并在該目錄中找到本程序?qū)?yīng)報(bào)名中的data.txt文件,該文件所在的目錄下圖所示,雙擊Device File Explorer視圖中的data.txt,即可Android Studio編輯框中查看data,txt文件中存儲(chǔ)的QQ賬號(hào)和密碼數(shù)據(jù),此時(shí)說(shuō)明存儲(chǔ)成功。
如需使用設(shè)備的文件系統(tǒng),請(qǐng)按以下步驟操作:
- 如需打開(kāi)設(shè)備瀏覽器,請(qǐng)依次選擇?View > Tool Windows > Device File Explorer,或點(diǎn)擊工具窗口欄中的?Device File Explorer??按鈕。
- 從列表中選擇設(shè)備。
- 在文件瀏覽器窗口中與設(shè)備內(nèi)容交互:
- 右鍵點(diǎn)擊某個(gè)文件或目錄即可創(chuàng)建新的文件或目錄。
- 保存、上傳、刪除所選文件或目錄,或?qū)⑵渫降侥挠?jì)算機(jī)。
- 雙擊某個(gè)文件可在 Android Studio 中將其打開(kāi)。
?可以找到Android Studio 模擬機(jī)上的所裝載的應(yīng)用程序的文件存儲(chǔ)內(nèi)容。?
??結(jié)尾
至此,文件存儲(chǔ)的相關(guān)知識(shí)已講解完成,該知識(shí)所用到的核心技術(shù)是利用I/O流來(lái)進(jìn)行文件讀寫(xiě)操作,其中,Context類(lèi)中提供的openFileInput()和OpenFileOutput()方法的用法,一定要掌握。?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-412778.html
??歡迎各位→點(diǎn)贊?? + 收藏?? + 留言???
??寫(xiě)給讀者:很高興你能看到我的文章,希望我的文章可以幫助到你,祝萬(wàn)事順意??????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-412778.html
到了這里,關(guān)于[Android Studio]Android 數(shù)據(jù)存儲(chǔ)-文件存儲(chǔ)學(xué)習(xí)筆記-結(jié)合保存QQ賬戶(hù)與密碼存儲(chǔ)到指定文件中的演練的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!