基础学习总结(三)--文本、SD卡数据读写

简单的文本数据写入文件不需要权限,读写SD卡在4.0版本前需要写权限、在4.0后需要读写权限

布局:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <EditText android:id="@+id/et_number"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:phoneNumber="true"
12         android:hint="请输入手机号" />
13 
14     <EditText
15         android:id="@+id/et_pwd"
16         android:layout_width="fill_parent"
17         android:layout_height="match_parent"
18         android:hint="请输入密码"
19         android:inputType="textPassword" />
20     
21     <CheckBox android:id="@+id/cb_remerber_pwd"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:checked="true"
25         android:text="记住密码" />
26     
27     <Button android:id="@+id/btn_Login"
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:text="登陆"
31          />
32     
33 </LinearLayout>
View Code

Manifest.xml添加权限

1 <!-- 写入外部设备(sdka)的权限 -->
2     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

MainActivity:

 1 public class MainActivity extends Activity implements OnClickListener {
 2     private static final String TAG = "MainActivity";
 3     private EditText etNumber;
 4     private EditText etPwd;
 5     private CheckBox cbRemerber;
 6     
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         //获取控件中数据
12         etNumber = (EditText)findViewById(R.id.et_number);
13         etPwd=(EditText)findViewById(R.id.et_pwd);
14         cbRemerber=(CheckBox)findViewById(R.id.cb_remerber_pwd);
15         Button btnLogin = (Button)findViewById(R.id.btn_Login);
16         //添加点击事件
17         btnLogin.setOnClickListener(this);
18         //获取SD卡数据
19         Map<String,String> userInfo=UtilsOfSDCard.getUserInfo(this);
20         if(userInfo!=null){
21             etNumber.setText(userInfo.get("number"));
22             etPwd.setText(userInfo.get("pwd")); 
23         }
24     }
25 
26     @Override
27     public void onClick(View v) {
28         // TODO Auto-generated method stub
29         //1.取出账号,密码
30         String Number=etNumber.getText().toString();
31         String Pwd=etPwd.getText().toString();
32         //判断是否有值,弹出吐司
33         if(TextUtils.isEmpty(Number) || TextUtils.isEmpty(Pwd)){
34             Toast.makeText(this, "请正确数据账号、密码", Toast.LENGTH_SHORT).show();
35             return;
36         }
37         //2.判断cb是否被选中
38         if(cbRemerber.isChecked()){
39             Log.i(TAG, "记住密码"+Number+","+Pwd);
40             boolean isSuccess=UtilsOfSDCard.saveUserInfo(this,Number, Pwd);
41             if(isSuccess)
42                 Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
43             else
44                 Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
45         }
46         
47         //3.登陆成功
48         Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
49     }
50 
51     
52 }
View Code

UtilsOfSDCard类:

 1 public class UtilsOfSDCard {
 2     //保存信息到sd卡
 3     public static boolean saveUserInfo(Context context, String number,String pwd){
 4         try {
 5             //获取SD卡状态
 6             String state=Environment.getExternalStorageState();
 7             //判断SD卡状态
 8             if(!Environment.MEDIA_MOUNTED.equals(state))
 9             {
10                 return false;
11             }
12             //动态获取SD卡路径
13             File sdCardFile = Environment.getExternalStorageDirectory();
14             File f=new File(sdCardFile,"qqLogin1.txt");
15             //写入流
16             FileOutputStream fos=new FileOutputStream(f);
17             
18             String data=number+"##"+pwd;
19             fos.write(data.getBytes());
20             fos.flush();
21             fos.close();
22             return true;
23         } catch (Exception e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         }
27         
28         return false;
29     }
30     
31     //返回信息
32     public static Map<String,String> getUserInfo(Context context){
33         try {
34             String state=Environment.getExternalStorageState();
35             if(!Environment.MEDIA_MOUNTED.equals(state))
36             {
37                 return null;
38             }
39             File sdCartFile=Environment.getExternalStorageDirectory();
40             File f=new File(sdCartFile,"qqLogin1.txt");
41             BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
42             String text=br.readLine();
43             br.close();
44             if(!TextUtils.isEmpty(text)){
45                 Map<String,String> userInfoMap=new HashMap<String,String>();
46                 String[] split = text.split("##");
47                 userInfoMap.put("number", split[0]);
48                 userInfoMap.put("pwd", split[1]);
49                 Log.w("UtilsOfSDCard", split[0]+","+split[1]);
50                 return userInfoMap;
51             }
52             
53         } catch (Exception e) {
54             // TODO Auto-generated catch block
55             e.printStackTrace();
56         }
57         
58         return null;
59     }
60 }
View Code

Utils类:

 1 public class Utils {
 2     /*
 3      * 保存用户信息
 4      * */
 5     public static boolean saveUserInfo(String number,String pwd){
 6         
 7         try {
 8             String path="/data/data/com.cui.qqlogin/QQLogin.txt";
 9             //写入流
10             FileOutputStream fs=new FileOutputStream(path);
11             String data=number+"##"+pwd;
12             fs.write(data.getBytes());
13             fs.flush();
14             fs.close();
15             return true;
16         } catch (Exception e) {
17             // TODO Auto-generated catch block
18             e.printStackTrace();
19         }
20         
21         return false;
22     }
23     
24 public static boolean saveUserInfo(Context context, String number,String pwd){
25         
26         try {
27             //动态获取文件路径
28             File filesDir = context.getFilesDir();
29             File f=new File(filesDir,"QQLogin.txt");
30             //写入流
31             FileOutputStream fs=new FileOutputStream(f);
32             String data=number+"##"+pwd;
33             fs.write(data.getBytes());
34             fs.flush();
35             fs.close();
36             return true;
37         } catch (Exception e) {
38             // TODO Auto-generated catch block
39             e.printStackTrace();
40         }
41         
42         return false;
43     }
44     
45     public static Map<String,String> getUserInfo(){
46         try {
47             String path="/data/data/com.cui.qqlogin/QQLogin.txt";
48             //输出流
49             FileInputStream fis=new FileInputStream(path);
50             //字符流
51             BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
52             String text = reader.readLine();
53             if(!TextUtils.isEmpty(text)){
54                 String[] split=text.split("##");
55                 Map<String,String> userofMap=new HashMap<String,String>();
56                 userofMap.put("number", split[0]);
57                 userofMap.put("pwd", split[1]);
58                 return userofMap;
59             }
60             
61         } catch (Exception e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         }
65         
66         return null;
67     }
68     
69     public static Map<String,String> getUserInfo(Context context){
70         try {
71             //String path="/data/data/com.cui.qqlogin/QQLogin.txt";
72             //动态获取文件路径
73             File filesDir = context.getFilesDir();
74             File f=new File(filesDir,"QQLogin.txt");
75             //输出流
76             FileInputStream fis=new FileInputStream(f);
77             //字符流
78             BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
79             String text = reader.readLine();
80             reader.close();
81             if(!TextUtils.isEmpty(text)){
82                 String[] split=text.split("##");
83                 Map<String,String> userofMap=new HashMap<String,String>();
84                 userofMap.put("number", split[0]);
85                 userofMap.put("pwd", split[1]);
86                 return userofMap;
87             }
88             
89         } catch (Exception e) {
90             // TODO Auto-generated catch block
91             e.printStackTrace();
92         }
93         
94         return null;
95     }
96 }
View Code

 

====================插播快捷键

Ctrl+N 快速新建
Ctrl+H 选中项目中搜索
Ctrl+L 跳转到指定行
Ctrl+F 在文本中查找内容

====================

文件存储地址:

this.getFilesDir(); // /data/data/包名/files
this.getCacheDir(); // /data/data/包名/cache
openFileOutput("aa.txt", 0); /data/data/包名/files/aa.txt
context.getSharedPreferences("cuidemo", context.MODE_PRIVATE); /data/data/包名/shared_prefs/cuidemo.xml

 1 public class UtilsOfSharedPreferences {
 2     //保存信息到sd卡
 3     public static boolean saveUserInfo(Context context, String number,String pwd){
 4         try {
 5     sp=context.getSharedPreferences("cuidemo", context.MODE_PRIVATE);
 6             //获得一个编辑对象
 7             Editor edit = sp.edit();
 8             edit.putString("number", number);
 9             edit.putString("pwd", pwd);
10             edit.commit();
11             return true;
12         } catch (Exception e) {
13             // TODO Auto-generated catch block
14             e.printStackTrace();
15         }
16         return false;
17     }
18     
19     //返回信息
20     public static Map<String,String> getUserInfo(Context context){
21         SharedPreferences sp=context.getSharedPreferences("cuidemo", context.MODE_PRIVATE);
22         String number=sp.getString("number", null);
23         String pwd=sp.getString("pwd", null);
24         
25         if(!TextUtils.isEmpty(number)&& !TextUtils.isEmpty(pwd)){
26             Map<String,String> userInfoMap=new HashMap<String,String>();
27             userInfoMap.put("number", number);
28             userInfoMap.put("pwd", pwd);
29             return userInfoMap;
30         }
31         
32         return null;
33     }
34 }
View Code

 

权限相关:
1. 私有文件
Context.MODE_PRIVATE
2. 可读文件
Context.MODE_WORLD_READABLE
3. 可写文件
Context.MODE_WORLD_WRITEABLE
4. 可读可写文件.
Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE

 

权限二进制图解:

posted @ 2015-06-16 07:42  大胖儿在努力  阅读(544)  评论(0编辑  收藏  举报