Android内部存储示例教程
今天我们将探讨Android的内部存储。Android提供了几种结构化的数据存储方式。其中包括
- Shared Preferences
- Internal Storage
- External Storage
- SQLite Storage
- Storage via Network Connection(on cloud)
在本教程中,我们将探讨如何使用Android内部存储将数据保存到文件中并进行读取。
安卓手机内部存储
Android内部存储是设备内存上的私有数据存储。默认情况下,将文件保存和加载到内部存储区域是仅对该应用程序私有的,其他应用程序无法访问这些文件。当用户卸载应用程序时,与该应用程序相关的内部存储文件也会被删除。然而,请注意一些用户会对他们的Android手机进行root授权,从而获得超级用户访问权限。这些用户可以读取和写入任何文件。
在Android内部存储中读写文本文件
Android提供了openFileInput和openFileOutput函数,可以在Java I/O类中使用这些函数来修改读写本地文件的流。
- openFileOutput(): This method is used to create and save a file. Its syntax is given below:
FileOutputStream fOut = openFileOutput(“file name”,Context.MODE_PRIVATE);The method openFileOutput() returns an instance of FileOutputStream. After that we can call write method to write data on the file. Its syntax is given below:
String str = “test data”;
fOut.write(str.getBytes());
fOut.close(); - openFileInput(): This method is used to open a file and read it. It returns an instance of FileInputStream. Its syntax is given below:
FileInputStream fin = openFileInput(file);After that, we call read method to read one character at a time from the file and then print it. Its syntax is given below:
int c;
String temp=””;
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}fin.close();
In the above code, string temp contains all the data of the file.
- Note that these methods do not accept file paths (e.g. path/to/file.txt), they just take simple file names.
Android内部存储项目结构
Android内部存储示例代码
xml布局中包含一个EditText用于向文件写入数据,以及一个写入按钮和读取按钮。注意,点击事件的方法仅在xml文件中定义,如下所示:activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="Android Read and Write Text from/to a File"
android:textStyle="bold"
android:textSize="28sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="22dp"
android:minLines="5"
android:layout_margin="5dp">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write Text into File"
android:onClick="WriteBtn"
android:layout_alignTop="@+id/button2"
android:layout_alignRight="@+id/editText1"
android:layout_alignEnd="@+id/editText1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read Text From file"
android:onClick="ReadBtn"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/editText1"
android:layout_alignStart="@+id/editText1" />
</RelativeLayout>
MainActivity中包含了如上所述的读写文件的实现。
package com.Olivia.internalstorage;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends Activity {
EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg=(EditText)findViewById(R.id.editText1);
}
// write text to file
public void WriteBtn(View v) {
// add-write text into file
try {
FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textmsg.getText().toString());
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
// Read text from file
public void ReadBtn(View v) {
//reading text from file
try {
FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
textmsg.setText(s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当数据成功写入内部存储时,将显示一个祝酒词,并且在从文件中读取数据时,数据将在EditText本身中显示。下面显示的图像是项目的输出。图像描述了文本被写入内部存储,并且在点击“读取”后,将以相同的EditText方式显示出来。
文件存放在哪里?
要查看文件,从工具->Android->Android设备监视器中打开Android设备监视器。如下图所示,文件位于data->data->{包名}->files文件夹中。文件“mytextfile.txt”位于项目包名com.Olivia.internalstorage中,如下图所示。从下面的链接下载Android内部存储示例的最终项目。
下载Android内部存储示例项目