SD存储读取

 public  void bt7(View v){
        //判断sd卡是否挂载
       if( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
       {
           //得到文本框内容
           String ss=et.getText().toString();
           try {
           //写入
           //1.构造输出流
           //1)得到路径
               //得到SD卡根目录
               //String path=Environment.getExternalStorageDirectory().getCanonicalPath();
               //得到包名对应的目录
               String path=getExternalFilesDir("Music").toString();
               Toast.makeText(MainActivity.this, "path="+path, Toast.LENGTH_LONG).show();

               //构造文件
           FileOutputStream fos=new FileOutputStream(path+"/test.txt");
               PrintStream ps=new PrintStream(fos);
               ps.print(ss);
               ps.close();
               fos.close();
               Toast.makeText(MainActivity.this, "写入外部成功", Toast.LENGTH_SHORT).show();

           }
           catch (Exception e){

               e.printStackTrace();
               Toast.makeText(MainActivity.this, "存储文件出错", Toast.LENGTH_SHORT).show();
           }

       }else {
           Toast.makeText(MainActivity.this, "SD卡未加载", Toast.LENGTH_SHORT).show();
       }

    }
    public  void bt8(View v){
        if( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            try {
                String path=getExternalFilesDir("Music").getCanonicalPath()+"/test.txt";
                FileInputStream fis=new FileInputStream(path);
                byte []b=new byte[1024];
                int i;
                String ss="";
                while ((i=fis.read(b))>0){
                    ss+=new String(b,0,i);
                }
                Toast.makeText(MainActivity.this, "文件内容"+ss, Toast.LENGTH_SHORT).show();
                fis.close();
            }
            catch (Exception e){
                Toast.makeText(MainActivity.this, "读取文件出错", Toast.LENGTH_SHORT).show();
            }

        }

        else {
            Toast.makeText(MainActivity.this, "SD卡未加载", Toast.LENGTH_SHORT).show();
    }
    }
原文地址:https://www.cnblogs.com/storm47/p/5536821.html