您的位置:首页 > 移动开发 > Android开发

Android 存储学习之在内部存储中读写文件

2015-08-02 17:09 525 查看
大家都知道,在实际工作中开发,进场需要将一个文件写入到手机的存储中。既然说到手机的存储空间,那就先说说手机的存储空间分类:

1: 内部存储空间

        RAM内存: 也就是手机的运行的内存,相当于电脑的内存

       ROM内存: 也就是手机的存储内存,相当于电脑的硬盘

2: 外部存储空间

       也就是可插拔的SD卡,相当于电脑的移动硬盘,U盘等。

既然手机存储分为两类,那我们先看看如何在手机的内部存储中读写文件。

那我们引入一个实际中很常见的例子:



一般带登录界面的,都需要将用户的信息保存到本地。我们就用上面的例子演示,将用户输入的信息保存到手机的内部存储。

布局文件代码如下:
<LinearLayout 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"
tools:context="${relativePackage}.${activityClass}"
android:orientation="vertical"
>

<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>

<EditText
android:id="@+id/ed_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:inputType="textPassword"
/>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"

/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginRight="40dp"
android:layout_alignParentRight="true"
android:onClick="login"
/>

</RelativeLayout>

</LinearLayout>


当我们点击按下确定按钮后,会将我们的用户信息,写到手机的内部存储中,也就是该应用所在的包名下
public void login(View v)
{
String name = ed_nam.getText().toString();

String passwd = ed_passwd.getText().toString();

//判断用户名或者密码是否输入
if((name.equals("")) || (passwd.equals("")))
{
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
}
else
{
//如果选中自动登录,我们就需要保存用户名和密码
if(cb.isChecked())
{
//创建一个文件,用户保存用户名和密码
File file = new File("data/data/com.demo.storage/info.txt");
try {

FileOutputStream fos = new FileOutputStream(file);
//写入用户名和密码,以name##passwd的格式写入
fos.write((name + "##" + passwd).getBytes());
//关闭输出流
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
}

我所指定的文件存储路径就是包名下的info.txt文件:



我们导出可以看下:



到这里的话,我们的往手机的内部存储中写已经完成。接下是往手机的内部存储中读。

那我们就这样做,当如果只要点击进入此Actiivty就显示出用于以前保存的用于和密码

public void readInfo()
{
File file = new File("data/data/com.demo.storage/info.txt");

//如果文件存在,则读取
if(file.exists())
{
try {

FileInputStream fin = new FileInputStream(file);
//把字节流转化为字符流
BufferedReader buffer = new BufferedReader(new InputStreamReader(fin));
//读取文件中的用户名和密码
String text = buffer.readLine();
//以##为关键字分割字符
String s[] = text.split("##");

//设置进EditText
ed_nam.setText(s[0]);
ed_passwd.setText(s[1]);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

当我们只要进入就会回显出,我能输入的用户名和密码:



到这里我们的读写就完成了。

但是有人有没有发现我们上面的路径都是我们自己写死的,同时也是自己用手一个一个写的,万一中间写错一个字母,岂不是很麻烦。

不过Google给我们提供了一个专门访问内部存储的API:getFilesDir()

//创建一个文件,用户保存用户名和密码
//File file = new File("data/data/com.demo.storage/info.txt");

//getFilesDir返回的是一个File对象,其路径是:data/data/com.demo.storage/files
File file = new File(getFilesDir(), "info.txt");getFileDir的路径是包名下files文件下:当我们点击登录时,就会在此路径下创建一个info.txt文件



同理Google也提供了一个API: getCacheDir(),其路径大家可以猜到了,就是上图cache所在的路径了。

我们进入到系统设置,app,正在运行中就找到此进程



就能看到如上图所示,其中Clear Data按钮是清除包名文件夹下所有的内容,Clear Cache是清除Cache文件夹下的内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息