说到Android中的存储方式,大家应该都会想到手机的文件存储、sd卡存储、sp存储、数据库存储;
其实之前我已经写过一次,但是当时编辑的比较凌乱,看起来不太方便,所以现在重新写四大存储方式并进行讲解,此本主讲文件的存储与读取
,闲话少说,Code送上~
因为涉及到文件的读写问题,所以首先需要申明权限,在正式开发中最好适配6.0、7.0的特性
<uses-permission android:name="android.permission.WRITE_PROFILE"/>
<uses-permission android:name="android.permission.READ_PROFILE"/>
提示:因文件存储方式大多存储的为文本信息,并不一定能完全满足用户需求,所以可以根据自身需求酌情考虑使用上方提到的其他存储方式
使用方式与注意事项
- 使用openFileOutput存储,openFileInput读取
- 输入的时候为写入,所以使用的是Output,输出的时候为读取,使用的Input
- 对应的使用BufferedWrite与BufferedRead,之后传入上文的FileOutputStream与FileInputStream
- BufferedRead因为要读取,所有记得使用StringBuilder的容器
- io流使用后,要记得close,避免内存泄漏
实现效果
- 前置效果
- 实现效果
- 最终效果
具体实现
MainActivity
package com.example.storagedispose;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private EditText mEdit;
private TextView mContent;
private FileOutputStream openFileOutput;
private BufferedWriter bufferedWriter;
private FileInputStream openFileInput;
private BufferedReader bufferedReader;
private StringBuilder builderContent;
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mContent = (TextView) findViewById(R.id.main_content);
mEdit = (EditText) findViewById(R.id.main_edit);
Button mSave = (Button) findViewById(R.id.save_one);
Button mLoad = (Button) findViewById(R.id.load_one);
mSave.setOnClickListener(this);
mLoad.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.save_one:
data = mEdit.getText().toString();
onsave(data);
break;
case R.id.load_one:
onload();
break;
default:
break;
}
}
// 文件的存儲
private void onsave(String data) {
// 将数据存储在本地文件中
try {
// 将数据存储于指定的文件-第一个参数为文件名,第二个参数确定写入方式
openFileOutput = openFileOutput("save_one", MODE_PRIVATE);
// 创建字符输入流,吧上方的文件内容写进去
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
openFileOutput));
// 将我们输入的数据保存到这个文件夹里面
bufferedWriter.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 这里的执行完全是为了避免内存的泄漏
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 进行文件读取,直接抛出的总异常
private void onload() {
// 读取手机文件名为--save_one的文件
try {
openFileInput = openFileInput("save_one");
// 字符读取流
bufferedReader = new BufferedReader(new InputStreamReader(
openFileInput));
// 创建StringBuilder用于存储数据
builderContent = new StringBuilder();
// 查看是否有数据,有的话直接添加到我们的容器中
String line = null;
while ((line = bufferedReader.readLine()) != null) {
builderContent.append(line);
mContent.setText(builderContent);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
activity.xml
<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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/main_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="在此输入要保存的信息"
/>
<Button
android:id="@+id/save_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件保存" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/load_one"
android:text="读取文件的数据"
android:gravity="center"
/>
<TextView
android:id="@+id/main_content"
android:text="读取数据展示区"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
数据查询
因此blog写的时候较早,使用的是DDMS的工具,其实在后面的时光里我们可以通过插件或者三方工具,获取到存储的文件信息 ~
- 通过DDMS导出数据,进行数据查询
2.查看导出的数据(我们发现数据是没有问题的哦,在此写入成功)