Background
Some things can be stored in your own defined file. This file can be stored on your mobile phone or on the SD card. Here we will mainly introduce the storage and reading of things on the SD card ~
Code
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 Wood", Toast. LENGTH_LONG ). show () ;}} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); Toast. makeText (context, "Wrong", Toast. LENGTH_LONG ). show (); try {fos. close ();} catch (IOException e1) {// catch Block e1.printStackTrace () generated automatically by TODO ();}}
}}
The above is the stored code, where Environment. MEDIA_MOUNTED is used to check whether mounting is performed.
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 FCM = new FileInputStream (file); BufferedReader br = new BufferedReader (new InputStreamReader (FCM); 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) {// catch Block e automatically generated by TODO. printStackTrace (); return null ;}finally {}}}
The above is the read code. It will be saved and read. If it is saved, it can be read out ~
I am the dividing line of tiantiao
Source code: http://pan.baidu.com/s/1dD1Qx01
SDcard.zip
Reprinted please indicate the source: http://www.cnblogs.com/yydcdut/p/3720648.html