Android的文件存储

//文件的写入

String content1 = edt_file.getText().toString();

//用于文件的写操作
FileOutputStream fos=null;

//缓冲输入流
BufferedWriter writer = null;
try {
//如果文件存在,直接打开文件,如果不存在,创建文件
//openFileOutput(String name,int mode)方法:用于向当前应用文件夹下输出文件,并返回FileOutputStream输出流。
fos = openFileOutput("test.txt", Context.MODE_PRIVATE);

//OutputStreamWriter作用是缓冲输入流与文件写操作的关联
writer = new BufferedWriter(new OutputStreamWriter(fos));
//原来BufferedWriter是缓冲输入流,意思是当你调用BufferedWriter的write方法时候。
//数据是先写入到缓冲区里,并没有直接写入到目的文件里。你必须调用BufferedWriter的flush()方法。
//这个方法会刷新一下该缓冲流,也就是会把数据写入到目的文件里。或者你可以调用BufferedWriter的close()方法,
//该方法会在关闭该输入流之前先刷新一下该缓冲流。也会把数据写入到目的文件里
writer.write(content1); //写入缓冲区
writer.flush(); //必须要写,要不然不会保存在文件中,只会在缓冲区中。
Toast.makeText(SharedPerferencesActivity.this, "写入文件完成", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(writer != null)
{
writer.close(); //关闭缓冲输入流
}
if(fos != null)
{
fos.close(); //关闭写操作
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

 

//文件的读取

//缓冲输出流
BufferedReader reader=null;

StringBuilder sb = new StringBuilder();
try {

//openFileInput(String name)方法:用于读取当前应用文件夹下的文件,并返回FileInputStream输入流。
reader = new BufferedReader(new InputStreamReader(openFileInput("test.txt")));
String content2 = reader.readLine();//读出行数据
while(content2 != null)//循环读出数据
{
sb.append(content2);
content2 = reader.readLine();
}
edt_file.setText(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(reader != null)//关闭缓冲输出流的数据
{
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Raw资源的读取:

//原始资源文件位于res/raw目录下,程序部署时会将其完整地打包进apk,在程序中可通过资源ID访问。
InputStream is = getResources().openRawResource(R.raw.rawtest);
BufferedReader reader2 =null;
StringBuffer sb2 = new StringBuffer();
try {

reader2 = new BufferedReader(new InputStreamReader(is,"GBK"));
String content2 = reader2.readLine();
while(content2 != null)
{
sb2.append(content2);
content2 = reader2.readLine();
}
edt_file.setText(sb2.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(reader2 != null)
{
reader2.close();
}

if(is != null)
{
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//SD卡的写入

//1.判断SD状态
FileOutputStream fos1 = null;
try {
String statue = Environment.getExternalStorageState();
if(statue.equals(Environment.MEDIA_MOUNTED))
{
//SD卡可用
//Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡可用...", Toast.LENGTH_LONG).show();
//得到路径
File dir = Environment.getExternalStorageDirectory();
//Toast.makeText(SharedPerferencesActivity.this, dir.getCanonicalPath(), Toast.LENGTH_LONG).show();
File file = new File(dir,"test.txt");
fos1 = new FileOutputStream(file);
Write(fos1);
}
else
{
Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡有问题...", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(fos1 !=null)
{
fos1.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//SD可的读取

//1.判断SD状态
FileInputStream fis = null;
try {
String statue1 = Environment.getExternalStorageState();
if(statue1.equals(Environment.MEDIA_MOUNTED))
{
//SD卡可用
//Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡可用...", Toast.LENGTH_LONG).show();
//InputStream
File dir2= Environment.getExternalStorageDirectory();
File file2 = new File(dir2,"test.txt");
fis = new FileInputStream(file2);
Read(fis);
}
else
{
Toast.makeText(SharedPerferencesActivity.this, "当前手机的SD卡有问题...", Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(fis != null)
{
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

posted @ 2016-06-04 14:29  黑暗时代地表人  阅读(199)  评论(0编辑  收藏  举报