Android系统一共提供了四种本地数据存储方式。分别是:SharePreference、SQLite、Content Provider和File。
这里我们将要介绍文件存储方式,文件存储简单的来说就是一般的JAVASE中的IO流,只是把他应用于Android手机中而已。文件的存储位置是在data/data/包名/files文件夹下面,如图:
知识点比较简单,直接演示Demo:
布局文件:
<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"
tools:context="com.itman.filedemo.MainActivity" >
<Button
android:id="@+id/bt_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bt_show"
android:layout_alignParentTop="true"
android:text="保存" />
<Button
android:id="@+id/bt_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_show_dou"
android:layout_centerHorizontal="true"
android:text="取出" />
<Button
android:id="@+id/bt_show_dou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/bt_save"
android:layout_below="@+id/bt_save"
android:text="文件" />
</RelativeLayout>
MainActivity.java程序:
public class MainActivity extends ActionBarActivity implements OnClickListener {
private static final String TAG = "MainActivity";
private Button bt_save;
private Button bt_show;
private Button bt_show_dou;
private byte[] light_code;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_save = (Button) findViewById(R.id.bt_save);
bt_show = (Button) findViewById(R.id.bt_show);
bt_show_dou = (Button) findViewById(R.id.bt_show_dou);
bt_save.setOnClickListener(this);
bt_show.setOnClickListener(this);
bt_show_dou.setOnClickListener(this);
light_code = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_save:
createFileWithByte(light_code, new StringBuilder("file"));
break;
case R.id.bt_show:
Toast.makeText(
this,
Arrays.toString(readFileWithByte(new StringBuilder("file"))),
Toast.LENGTH_SHORT).show();
break;
case R.id.bt_show_dou:
show();
break;
}
}
private void show() {
File file = new File(getBaseContext().getFilesDir().toString());
String[] name = file.list();
Toast.makeText(this, Arrays.toString(name), Toast.LENGTH_SHORT).show();
}
/**
* 创建文件并存储数据
* byte[] bytes 要存储的数据
* StringBuilder str 文件名称
*/
public void createFileWithByte(byte[] bytes, StringBuilder str) {
/**
* 创建File对象,其中包含文件所在的目录以及文件的命名
*/
File file = new File(this.getFilesDir(), str.append(".txt").toString());
// 创建FileOutputStream对象
FileOutputStream outputStream = null;
try {
// 如果文件存在则删除
if (file.exists()) {
file.delete();
}
// 获取FileOutputStream对象
outputStream = new FileOutputStream(file);
// 获取BufferedOutputStream对象
// 往文件所在的缓冲输出流中写byte数据
outputStream.write(bytes);
// 刷出缓冲输出流,该步很关键,要是不执行flush()方法,那么文件的内容是空的。
} catch (Exception e) {
// 打印异常信息
e.printStackTrace();
} finally {
// 关闭创建的流对象
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 取出制定文件的数据
*/
public byte[] readFileWithByte(StringBuilder str) {
byte[] light = new byte[10];
File file = new File(this.getFilesDir(), str.append(".txt").toString());
// 创建FileOutputStream对象
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
inputStream.read(light);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭创建的流对象
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return light;
}
}
运行结果:(运行之后先点击保存,左图是文件名称,右图是文件存储的数据)