使用官方提供的API获取sd的路径进行存储

class MyOnClick implements OnClickListener {

  @Override   public void onClick(View v) {

    // 判断sd的状态

    // MEDIA_REMOVED:sd卡不存在

    // MEDIA_MOUNTED:sd卡存在,但是没有挂载

    // MEDIA_CHECKING:sd卡正在遍历

    // MEDIA_MOUNTED:sd卡可用

       // MEDIA_MOUNTED_READ_ONLY:sd卡可用,但是只读  

     if (Environment.getExternalStorageState().equals(      Environment.MEDIA_MOUNTED)) {   

        // 用官方给的api获取sd卡的路径

           File file = new File(Environment.getExternalStorageDirectory()       + "indexs.ini");

           String names = name.getText().toString();     String nambers = namber.getText().toString();

        try {    

          // 创建写的实例类    

          FileOutputStream fos = new FileOutputStream(file);     

         // 调用写的方法,把数据写的本地      fos.write((names + "&&" + nambers).getBytes());     

         // 关闭资源      

        fos.close();    

         } catch (Exception e) {     

           // TODO Auto-generated catch block      e.printStackTrace();    

          }

             System.out.println("登陆成功!");

         }

       }

   }

     // 回显数据的方法  public void huixian() {

    // 用官方给的api获取sd卡的路径   File file = new File(Environment.getExternalStorageDirectory()     + "indexs.ini");

    if (file.exists()) {         

         try {    

         // io流转换

         FileInputStream fis = new FileInputStream(file);

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

         // 读取一行     

        String str = br.readLine();

         // 从&&处分开

          String[] s = str.split("&&");  

            // 设置回显数据    

           name.setText(s[0]);    

         namber.setText(s[1]);  

           br.close();   

      } catch (Exception e) {  

         // TODO Auto-generated catch block     e.printStackTrace();  

        }   

    }

   }

}

posted @ 2015-11-05 10:58  随笔、  阅读(227)  评论(0编辑  收藏  举报