是什么
文件存储是Android中最基本的一种数据存储方式。
怎么样
它不对存储的内容进行任何格式化的处理,所有的数据都是原封不动的存储到文件中,因而适合存储一些简单的文本数据或者二进制数据
怎么用
内部存储
文件存储的核心技术:使用Context类中提供的openFileInput()
和openFileOutput()
方法
把数据存入文件中
基础使用
在布局中放了一个EditText用于输入要保存的数据,和一个保存数据的按钮和一个读取数据的按钮
其中,保存数据按钮中的代码为:
String str = mEditText.getText().toString().trim();
save(str);
save(str)的具体实现为:
private void save(String str) {
BufferedWriter bw = null;
try {
//打开文件输出流,参数1:指定保存数据流的文件名称,如果不存在则自动创建它
FileOutputStream data = openFileOutput("data", MODE_PRIVATE);
//包装成字符流
OutputStreamWriter osw = new OutputStreamWriter(data);
//包装成缓冲字符流
bw = new BufferedWriter(osw);
//把要保存的数据写入到准备好的缓冲流中
bw.write(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流,原则:先打开的后关闭,后打开的先关闭
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出输入的概念是站在源的角度而不是站在文件的角度来说,因为是从源传输保存到文件,对于源来说是输出,所以使用输出流。所以,第一步,打开文件输出流,openFileOutput有两个参数 name和mode,源码解释
由图可知,name是指,要打开的文件的名称,也就是传输过来的数据要保存在这个名字的文件下。mode是存储的模式
文件的存储模式,主要有两种模式可以选择:MODE_PRIVATE(默认的操作模式):表示当指定同样文件名的时候,所写的内容将会覆盖原文件中的内容;MODE_APPEND:表示如果该文件已存在就往文件里面追加内容,不存在就创建新文件
。其他步骤在代码注释中有解释
使用DDMS,在系统文件夹下的data/data/com.cd.storage/files目录下查看保存的数据,如图
直接是纯文本,没有任何的格式
读取文件数据
读取按钮中的代码
String read = read();
Log.d("FileActivity", "读取出来的数据" + read);
read()的具体实现为
private String read() {
BufferedReader br = null;
StringBuffer sb = null;
try {
//获取到要读取的文件,参数:文件名
FileInputStream data = openFileInput("data");
//将文件流转换为字符流
InputStreamReader isr = new InputStreamReader(data);
//将字符流转换为缓冲流
br = new BufferedReader(isr);
//
//用于存储读取的每一行
String line = null;
//用于保存读取完的数据
sb = new StringBuffer();
//循环的读取
while ((line = br.readLine()) != null) {
sb.append(line);
}
//
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
input与output是成双成对的,保存数据使用output那么。读取数据必然是input,使用的是openFileInput参数为保存数据时指定的文件名,
点击读取文件按钮,可以看到如下输出
外部存储
向sdcard中写入数据
/**
* 该方法实现写入file文件,,,并且该文件被存储到sd卡里边
* @param fileName 文件名
* @param content 文件内容
* @throws Exception
*/
private void writeToSdcard(String filename, String content) {
Log.d("FileActivity", "有sdcard");
//参数1:文件要存储的位置,参数2:存储的文件名
File file = new File(Environment.getExternalStorageDirectory()+"/"+filename);
if (!file.exists()) {
file.mkdirs();
}else {
try {
//转换成文件输出流
FileOutputStream fos = new FileOutputStream(file);
//将要保存的内容写入到文件输出流中
//方式一:转化为字节写入文件流中
fos.write(content.getBytes());
//方式二:包装成字符缓冲流,将字符写入缓冲流中
// OutputStreamWriter osw = new OutputStreamWriter(fos);
// BufferedWriter br = new BufferedWriter(osw);
// br.write(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
向sdcard中读取数据
private String readFromSDcard(String sky) {
ByteArrayOutputStream bos = null;
FileInputStream fis = null;
String result = null;
//在sdcard里面找到名为Sky的文件
File file = new File(Environment.getExternalStorageDirectory(), filename);
//将文件转化为文件流
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
bos = new ByteArrayOutputStream();
while (fis.read(buffer) != (-1)) {
bos.write(buffer);
}
result = bos.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null && fis != null) {
try {
bos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
调用
switch (v.getId()) {
case R.id.btn_save:
String str = mEditText.getText().toString().trim();
//判断是否有sdcard
boolean sdcard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
Log.d("FileActivity", "是否有sdcard" + sdcard);
if (sdcard) {
writeToSdcard(filename, str);
} else {
Toast.makeText(this, "请插入sdcard", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_read:
String sky = readFromSDcard("Sky");
Log.d("FileActivity", "读取sdcard中的数据数据" + sky);
break;
}