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

android菜鸟学习笔记17----Android数据存储(一)文件读写

2015-07-08 15:13 906 查看
假如有如下需求,要求能够记录用户输入的用户名和密码,下次登录时,能直接获取之前保存的用户名密码,并在相应的EditText中显示。

要保存用户输入的数据,最先想到的应该就是文件读写了。

通过对android应用打包安装过程的观察,发现每个应用安装之后,都会在/data/data/下新建一个与应用包名相同的目录,该应用的所有文件都存放在该目录下。

例如:

main_layout.xml代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="10dp"

android:orientation="vertical" >

<TextView

android:id="@+id/tv_info"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="25sp"

android:textColor="#00ff00"

android:text="@string/tv_info_text"/>

<EditText

android:id="@+id/et_username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/et_name_text"/>

<EditText

android:id="@+id/et_password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/et_password_text"

android:inputType="textPassword"/>

<CheckBox

android:id="@+id/cb_reme"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/cb_reme_text"

/>

<Button

android:id="@+id/btn_login"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/btn_login_text"/>

</LinearLayout>


FileUtils.java代码:

package cn.csc.utils;

import java.io.FileOutputStream;

public class FileUtils {

public static boolean writeFile(String data){

String path = "/data/data/cn.csc.file/userinfo.txt";

try {

FileOutputStream fos = new FileOutputStream(path);

fos.write(data.getBytes());

fos.close();

return true;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

return false;

}

}

}


MainActivity.java:

public class MainActivity extends Activity {

private EditText et_username;

private EditText et_password;

private CheckBox cb_reme;

private Button btn_login;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main_layout);

et_username = (EditText) findViewById(R.id.et_username);

et_password = (EditText) findViewById(R.id.et_password);

cb_reme = (CheckBox) findViewById(R.id.cb_reme);

btn_login = (Button) findViewById(R.id.btn_login);

btn_login.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

String username = et_username.getText().toString();

String password = et_password.getText().toString();

boolean checked = cb_reme.isChecked();

if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){

Toast.makeText(MainActivity.this, "用户名密码不能为空",Toast.LENGTH_SHORT).show();

return;

}

if(checked){

if(username.equals("dqrcsc")&&password.equals("abcdef")){

if(FileUtils.writeFile(username+"#"+password)){

Toast.makeText(MainActivity.this, "保存成功",Toast.LENGTH_SHORT).show();

}

else{

Toast.makeText(MainActivity.this, "保存失败",Toast.LENGTH_SHORT).show();

}

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

}else{

Toast.makeText(MainActivity.this, "登录失败",Toast.LENGTH_SHORT).show();

}

}

}

});

}

}


运行结果:





注意到该文件的权限,默认仅能被当前用户读写。

保存用户名,密码成功,还需要实现下次打开时,自动读取保存的用户名密码,对应的方法可以在onCreate()中调用,已实现打开时自动加载。

在FileUtils中添加读取文件的方法:

public static String readFile(){

String path = "/data/data/cn.csc.file/userinfo.txt";

try {

BufferedReader br = new BufferedReader(new FileReader(path));

String line = br.readLine();

br.close();

return line;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}


修改MainActivity.java中onCreate(),添加如下代码:

     String info = FileUtils.readFile();

if(TextUtils.isEmpty(info)){

return;

}

if(!info.contains("#")){

return;

}else{

String[] strs = info.split("#");

et_password.setText(strs[1]);

et_username.setText(strs[0]);

cb_reme.setChecked(true);

}


上述直接操作写死的文件路径,是存在问题的,如果我修改了当前应用的包名,然后去操作写死的文件,就会出现权限被拒绝的错误:

如Manifest.xml中package修改为cn.csc.file1,注意要修改Activity节点,name使用全限定名:cn.csc.file.MainActivity。

运行程序,出现如下错误:

java.io.FileNotFoundException: /data/data/cn.csc.file/userinfo.txt (Permission denied)



文件仍然存在,但是相对与当前应用,已属于别的用户所有,而该文件只能被所属用户进行读写操作。

当然,如果把userinfo.txt的权限修改下,还是可以读写的。

adb shell挂载linux文件系统,然后进入/data/data/file目录

然后chmod 666 userinfo.txt



然后,就可以直接让cn.csc.file1应用直接读写了。

其实,Context有可以获取当前包文件存储路径的API:

getFilesDir();//返回/data/data/包名/files目录

getCacheDir();//返回/data/data/包名/cache目录

由于是在FileUtils类中读写文件,要使用Context的API,则需要把Context作为参数传递:

在FileUtils中添加对原有读写文件方法的重载:

public static boolean writeFile(Context context, String data){

File filesDir = context.getFilesDir();

try {

FileOutputStream fos = new FileOutputStream(new File(filesDir, "userInfo.txt"));

fos.write(data.getBytes());

fos.close();

return true;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

return false;

}

}

public static String readFile(Context context){

File filesDir = context.getFilesDir();

try {

BufferedReader br = new BufferedReader(new FileReader(new File(filesDir, "userInfo.txt")));

String line = br.readLine();

br.close();

return line;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}


修改MainActivity.java,调用新添加的读写文件方法,把当前Activity实例作为Context参数传递:

FileUtils.writeFile(MainActivity.this,username+"#"+password)

String info = FileUtils.readFile(this);

重新运行程序:



cn.csc.file1目录下多出了一个空的目录files

然后,填写信息,点登录,保存数据



files目录下,便多出了userInfo.txt的文件

文件的默认权限总是-rw- --- ---,即只有该文件所有者可以读写,其他用户均不具有对该文件的读写权限,如何设置文件权限呢?

Context提供了两个方法:

abstract FileInputStream openFileInput(String name)

返回指向/data/data/包名/files/name文件的FileInputStream实例

abstract FileOutputStream openFileOutput(String name, int mode)

返回指向/data/data/包名/files/name文件的FileOutputStream实例,文件不存在时,创建该文件,并根据mode参数,设置文件的权限。

mode的取值可以为:

Context.MODE_PRIVATE:(实际值:0x00000000)只有所有者具有读写权限,文件已存在直接覆盖

Context.MODE_APPEND:(实际值:0x00008000)文件已存在,则追加

Context.MODE_WORLD_READABLE:(实际值:0x00000001)其他所有用户具有读权限

Context.MODE_WORLD_WRITEABLE:(实际值:0x00000002)其他所有用户具有写权限

可以将这些取值位或操作,然后传递给openFileOutput()方法,设置同时具有多个属性。

修改FileUtils中的两个文件读写方法:

public static boolean writeFile(Context context, String data){

try {

FileOutputStream fos = context.openFileOutput("userInfo.txt", Context.MODE_PRIVATE);

fos.write(data.getBytes());

fos.close();

return true;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

return false;

}

}

public static String readFile(Context context){

try {

BufferedReader br = new BufferedReader(new InputStreamReader(context.openFileInput("userInfo.txt")));

String line = br.readLine();

br.close();

return line;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}


修改mode字段,查看对应文件权限:

MODE_PRIVATE:



MODE_APPEND:



此时,点击登陆按钮,文件大小变成了26,存放了两条用户名及密码信息,可见,是在已存在文件中,追加内容。并且,文件权限变味了同组用户也可读写。

MODE_WORLD_READABLE:



所有用户均可读

MODE_WORLD_WRITEABLE:



所有用户均可写

MODE_WORLD_READABLE| MODE_WORLD_WRITEABLE:



所有用户均可读可写

MODE_APPEND| MODE_WORLD_READABLE| MODE_WORLD_WRITEABLE:



所有用户均可读可写,并且是追加模式,当文件存在时,向已存在文件中追加内容,而非覆盖操作。

注意:由于MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE这两种模式过于危险,易产生安全问题,在Android4.2中已然废弃。

20150709补充:

还有一个模式:Context.MODE_MULTI_PROCESS



一般用于多个进程对同一个文件的读写,同组用户对其都有读写权限,与MODE_APPEND不同的是每次都会覆盖已存在的文件,而不追加。

读写缓存中的文件

利用Context的getCacheDir();//返回/data/data/包名/cache目录,获取缓存的存放路径。

在FileUtils中添加两个方法:

public static boolean writeCache(Context context, String data){

File filesDir = context.getCacheDir();

try {

FileOutputStream fos = new FileOutputStream(new File(filesDir, "userInfo.txt"));

fos.write(data.getBytes());

fos.close();

return true;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

return false;

}

}

public static String readCache(Context context){

File filesDir = context.getCacheDir();

try {

BufferedReader br = new BufferedReader(new FileReader(new File(filesDir, "userInfo.txt")));

String line = br.readLine();

br.close();

return line;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}


在MainActivity调用这两个读写Cache的方法:

运行结果:



注意:文件和缓存的区别:



Clear cache会直接清空cache目录,并且不会弹出警告信息

Clear data则会先弹出警告窗口,确认之后,才会清空files目录。



一般优化工具,都会清空Cache目录,而不会直接清空files目录,所以常用的文件要保存在files目录中,而临时文件则可以考虑放在cache目录中。

读写SD卡中的文件

Environment类提供了几个很实用的方法:

static File getDataDirectory() /data/data/包名/files

static File getDownloadCacheDirectory() 下载缓存路径

static File getExternalStorageDirectory() SD卡路径

static String getExternalStorageState() SD卡状态

static File getRootDirectory() 根目录

测试上述方法输出:

              Log.i("Environment","getDataDirectory:"+Environment.getDataDirectory().getPath());

Log.i("Environment","getDownloadCacheDirectory:"+Environment.getDownloadCacheDirectory().getPath());

Log.i("Environment","getExternalStorageDirectory:"+Environment.getExternalStorageDirectory().getPath());

Log.i("Environment","getRootDirectory:"+Environment.getRootDirectory().getPath());

Log.i("Environment","getExternalStorageState:"+Environment.getExternalStorageState());




修改FileUtils添加两个读写SD卡的方法:

public static boolean writeSD(String data){

try {

String state = Environment.getExternalStorageState();

if(state.equals(Environment.MEDIA_MOUNTED)){

File ext = Environment.getExternalStorageDirectory();

FileOutputStream fos = new FileOutputStream(new File(ext,"userInfo.txt"));

fos.write(data.getBytes());

fos.close();

return true;

}

return false;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

return false;

}

}

public static String readSD(){

try {

String state = Environment.getExternalStorageState();

if(state.equals(Environment.MEDIA_MOUNTED)){

File ext = Environment.getExternalStorageDirectory();

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ext.getPath()+"/userInfo.txt")));

String line = br.readLine();

br.close();

return line;

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}


此外,写SD需要配置权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

运行结果:



注意:在读写SD卡之前,需要先检查SD卡的状态,已挂载才能进行读写操作。此外,读写SD卡需要在Manifest.xml中配置相关的读写权限。在4.0之前读取SD卡是不需要配置读SD卡的权限的,4.0之后,添加了SD卡读取保护的设置,如果用户设置了SD卡读取保护,则读取SD卡中的文件也需要配置权限的。所以,最好同时在Manifest.xml中配置上读写SD卡的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: