Android - 内部存储
Android 为应用程序提供了多种存储方式来存储其数据。 这些存储位置是共享首选项、内部和外部存储、SQLite 存储和通过网络连接的存储。
在本章中,我们将研究内部存储。 内部存储是设备内存上私有数据的存储。
默认情况下,这些文件是私有的,只有您的应用程序可以访问并在用户删除您的应用程序时被删除。
写入文件
为了使用内部存储在文件中写入一些数据,调用 openFileOutput() 方法并带有文件名和模式。 该模式可以是 private , public 等。 它的语法如下 −
FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);
方法 openFileOutput() 返回 FileOutputStream 的一个实例。 所以你在 FileInputStream 的对象中接收它。 之后,您可以调用 write 方法将数据写入文件。 它的语法如下 −
String str = "data"; fOut.write(str.getBytes()); fOut.close();
读取文件
为了从您刚刚创建的文件中读取,请使用文件名调用 openFileInput() 方法。 它返回 FileInputStream 的一个实例。 它的语法如下 −
FileInputStream fin = openFileInput(file);
之后,您可以调用 read 方法从文件中一次读取一个字符,然后您可以打印它。 它的语法如下 −
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
//string temp contains all the data of the file.
fin.close();
除了 write 和 close 方法外,FileOutputStream 类还提供了其他方法来更好地写入文件。 下面列出了这些方法 −
| 序号 | 方法 & 描述 |
|---|---|
| 1 |
FileOutputStream(File file, boolean append) 此方法构造一个写入文件的新 FileOutputStream。 |
| 2 |
getChannel() 此方法返回一个与此流共享其位置的只写 FileChannel |
| 3 |
getFD() 此方法返回底层文件描述符 |
| 4 |
write(byte[] buffer, int byteOffset, int byteCount) 此方法从位置 offset 开始的字节数组缓冲区中将 count 个字节写入此流 |
除了 read 和 close 方法外,FileInputStream 类还提供了其他方法来更好地读取文件。 下面列出了这些方法 −
| 序号 | 方法 & 描述 |
|---|---|
| 1 |
available() 此方法返回可以读取或跳过的估计字节数,而不会阻塞以获取更多输入 |
| 2 |
getChannel() 此方法返回一个与该流共享其位置的只读 FileChannel |
| 3 |
getFD() 此方法返回底层文件描述符 |
| 4 |
read(byte[] buffer, int byteOffset, int byteCount) 此方法从该流中读取最多 length 个字节,并将它们存储在从偏移量开始的字节数组中 |
示例
这是一个演示使用内部存储来存储和读取文件的示例。 它创建了一个基本的存储应用程序,允许您从内部存储读取和写入。
要试验这个示例,您可以在实际设备或模拟器中运行它。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android Studio IDE 在 com.example.sairamkrishna.myapplication 包下创建一个 Android 应用程序。 |
| 2 | 修改 src/MainActivity.java 文件添加必要的代码。 |
| 3 | 修改 res/layout/activity_main 以添加相应的 XML 组件 |
| 4 | 运行应用程序并选择一个正在运行的 android 设备并在其上安装应用程序并验证结果 |
以下是修改后的主活动文件src/MainActivity.java的内容。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button b1,b2;
TextView tv;
EditText ed1;
String data;
private String file = "mydata";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
ed1=(EditText)findViewById(R.id.editText);
tv=(TextView)findViewById(R.id.textView2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data=ed1.getText().toString();
try {
FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
tv.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
}
});
}
}
以下是 res/layout/activity_main.xml 的修改内容。
以下代码中的 abc 表示 tutorialspoint.com 的标志
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Internal storage" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter Text"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="42dp"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2"
android:textColor="#ff5bff1f"
android:textSize="25dp" />
</RelativeLayout>
以下是 res/values/string.xml 的内容。
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 文件的内容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
让我们尝试运行我们刚刚修改的存储应用程序。 我假设您在进行环境设置时已经创建了 AVD。要从 Android Studio 运行应用程序,请打开项目的一个活动文件,然后单击工具栏中的运行
图标。Android Studio 在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下 Emulator 窗口 −
现在您需要做的是在该字段中输入任何文本。 例如,我输入了一些文本。 按保存按钮。 以下通知将出现在您的 AVD 中 −
现在,当您按下加载按钮时,应用程序将读取文件并显示数据。 在我们的情况下,将返回以下数据 −
请注意,您实际上可以通过切换到 DDMS 选项卡来查看此文件。 在 DDMS 中,选择文件资源管理器并导航此路径。
tools>android>android device Monitor
这也显示在下图中。

