码迷,mamicode.com
首页 > 其他好文 > 详细

数据存储之--内存卡的存储

时间:2015-02-07 23:06:36      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:内存卡的存储

1,数据的保存

File file=new File(Environment.getExternalStorageDirectory(), name);

                  try {

                     FileOutputStream  fos=new FileOutputStream(file);

                     fos.write(content.getBytes());

                     fos.close();

                       Toast.makeText(getApplicationContext(),"保存成功", 200).show();

                  }catch (Exception e) {

                       Toast.makeText(getApplicationContext(),"保存失败", 200).show();

                      e.printStackTrace();

                  }

2,数据的读取

File file = new File(Environment.getExternalStorageDirectory(), name);      

              try {

                  FileInputStream fis= new FileInputStream(file);

                  BufferedReader br =new BufferedReader(new InputStreamReader(fis));

                  //读取文档的数据

                  String content =br.readLine();

                  //拆分字符串

                  String [] str =content.split("=");

               //显示数据

                  if(!content.isEmpty()){

                     et_name.setText(str[0]);

                     et_password.setText(str[1]);

                  }

              } catch (Exception e) {               

                  e.printStackTrace();

              }            

           }

       });

注意:保存数据记得注册

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE”/>

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


数据存储之--内存卡的存储

标签:内存卡的存储

原文地址:http://9882931.blog.51cto.com/9872931/1612714

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!