背景

一些东西可以存在自己定义的文件里面,这个文件可以在手机中,可以在SD卡中,在这里就主要介绍一下在SD卡中的存储和读取吧~

代码

public class save {    public static void savefile2card(Context context,String username,String password)    {        File file = null;        FileOutputStream fos = null;        try         {            if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))            {//    file = new File("/sdcard/info.txt");                file = new File(Environment.getExternalStorageDirectory(),"info.txt");                fos = new FileOutputStream(file);                fos.write((username+"!!!!"+password).getBytes());                }            else            {                Toast.makeText(context, "SD木有", Toast.LENGTH_LONG).show();            }        }         catch (Exception e)         {            // TODO 自动生成的 catch 块            e.printStackTrace();            Toast.makeText(context, "Wrong", Toast.LENGTH_LONG).show();            try             {                fos.close();            }             catch (IOException e1)             {                // TODO 自动生成的 catch 块                e1.printStackTrace();            }        }
    }}

上面是存的代码,这里面用到了Environment.MEDIA_MOUNTED,查看是否挂载。

public class read {    public static Map<String,String> getSaveFile(Context context)    {        //File file =new File(context.getFilesDir(),"info.txt");        File file = new File(Environment.getExternalStorageDirectory(),"info.txt");                try         {            FileInputStream fis = new FileInputStream(file);            BufferedReader br = new BufferedReader(new InputStreamReader(fis));            String str = br.readLine();            String[] infos = str.split("!!!!");            Map<String,String> map = new HashMap<String, String>();            map.put("username",infos[0]);            map.put("password", infos[1]);            br.close();            return map;        }         catch (Exception e)        {            // TODO 自动生成的 catch 块            e.printStackTrace();            return null;        }        finally        {                    }            }}

上面是读取的代码,会存就会读了,存得进去那么就读得出来撒~

源代码:http://pan.baidu.com/s/1dD1Qx01

SDcard.zip

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 一款常用的 Squid 日志分析工具
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  5. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  6. 【Android(安卓)系统开发】使用 Source InSight 阅读 Android(安
  7. android:使用audiotrack 类播放wav文件
  8. Mac 下载 编译 debug Android(安卓)源码
  9. 安装Android(安卓)Source【转】

随机推荐

  1. 开开心心爬APP,结果一坑连一坑
  2. nginx跳转 去掉工程名
  3. #6.1# 用python画出你的童年回忆
  4. centos7 中yum不能用了
  5. 看了我做的年度报表,老板大呼NB!
  6. 摆摊吧,后浪
  7. 用python爬取4332条粽子数据进行分析
  8. 一个公式三指标,电商分析的破局之道
  9. Python爬取 201865 条《隐秘的角落》弹幕
  10. LeetCode第 146 号问题: LRU 缓存机制