数据存储方式概述
Android中文件的访问权限
实例
关键代码
◆注意:Java中RandomAccessFile类支持随机访问文件内容,通过seek()定位文件位置。
FileDemo.zip
MainActivity.java
package com.bluelesson.filedemo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
int id = view.getId();
String name = null;
int mode = 0;
switch (id){
case R.id.btn_private:
name = "private.txt";
mode = Context.MODE_PRIVATE;
break;
case R.id.btn_append:
name = "append.txt";
mode = Context.MODE_APPEND;
break;
case R.id.btn_global_read:
name = "global_read.txt";
mode = Context.MODE_WORLD_READABLE;
break;
case R.id.btn_global_write:
name = "global_write.txt";
mode = Context.MODE_WORLD_WRITEABLE;
break;
case R.id.btn_global_rw:
name = "global_rw.txt";
mode = Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE;
break;
}
try {
FileOutputStream outputStream = openFileOutput(name,mode);
String content = "这是我的内容";
outputStream.write(content.getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
activity_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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_private"
android:text="私有方式"
android:textSize="20sp"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_append"
android:text="追加方式"
android:textSize="20sp"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_global_read"
android:text="全局读方式"
android:textSize="20sp"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_global_write"
android:text="全局写方式"
android:textSize="20sp"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_global_rw"
android:text="全局读写方式"
android:textSize="20sp"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
数据存储方式3:Shared Preferences
介绍
核心原理
关键代码
main1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt1"
android:hint="请在此输入密码"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_set"
android:text="保存口令"
android:textSize="20sp"
android:onClick="btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_get"
android:text="获取口令"
android:textSize="20sp"
android:onClick="btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.bluelesson.filedemo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
public void btnClick(View view) {
int id = view.getId();
switch(id){
case R.id.btn_set:
setPass();
break;
case R.id.btn_get:
getPass();
break;
}
}
private void getPass() {
// 1. 获取ShredPreferences对象
SharedPreferences sp = getSharedPreferences(
"lock", // xml文件的名称,文件夹:/data/data/<package name>/shared_prefs
Context.MODE_PRIVATE
);
// 2. 获取数据
String pass = sp.getString("pass","-1");
if(!pass.equals("-1")){
Toast.makeText(this,"口令:"+pass,Toast.LENGTH_SHORT).show();
}
}
private void setPass() {
// 1. 获取口令
EditText editText = findViewById(R.id.edt1);
String pass = editText.getText().toString();
// 2. 获取ShredPreferences对象
SharedPreferences sp = getSharedPreferences(
"lock", // xml文件的名称,文件夹:/data/data/<package name>/shared_prefs
Context.MODE_PRIVATE
);
// 3. 获取编辑器对象
SharedPreferences.Editor editor = sp.edit();
// 4. 压入数据
editor.putString("pass",pass);
// 5. 提交保存
editor.commit();
// 6. 提示一下
Toast.makeText(this,"保存完成",Toast.LENGTH_SHORT).show();
}