码迷,mamicode.com
首页 > 移动开发 > 详细

android 中在内存中保存文件

时间:2015-09-24 22:47:16      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:

以一个登陆的例子来将,将登陆时的信息保存在指定目录的文件中。在第一次登陆后,对用户名等在内存文件中保存。

android界面:

 

界面:

 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="match_parent"
 5     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     <TextView
13         android:layout_width="fill_parent"
14         android:layout_height="wrap_content"
15         android:text="@string/PlaseInputName" 
16         android:textSize="20dp"
17         />
18 
19     <EditText
20         android:id="@+id/et_username"
21         android:layout_width="fill_parent"
22         android:layout_height="wrap_content"
23         android:layout_marginBottom="20dp" 
24         android:textColor="#FFFF00"
25         />
26 
27     <TextView
28         android:layout_width="fill_parent"
29         android:layout_height="wrap_content"
30         android:text="@string/PlaseInputPwd"
31         android:textSize="20dp" />
32 
33     <EditText
34         android:inputType="textPassword"
35         android:id="@+id/et_userpwd"
36         android:layout_width="fill_parent"
37         android:layout_height="wrap_content" />
38 
39     <RelativeLayout
40         android:layout_width="fill_parent"
41         android:layout_height="wrap_content" >
42 
43         <CheckBox
44             android:id="@+id/cb_remeber_pwd"
45             android:layout_width="wrap_content"
46             android:layout_height="wrap_content"
47             android:checked="true"
48             android:text="@string/checkpwd" />
49 
50         <Button
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content" 
53             android:layout_alignParentRight="true"
54             android:onClick="login"
55             android:text="@string/login"
56             />
57     </RelativeLayout>
58 
59 </LinearLayout>

界面原型:技术分享

 

在第一次登陆时,看是否选择记住密码,如果是将登陆信息保存后读取

public class LoginService {
    public static boolean saveUerInfo(Context context, String UerName,
            String password) {

        try {
            File file = new File(context.getFilesDir(), "info.txt");
            // File file = new File("/data/data/com.example.login/info.txt");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write((UerName + "###" + password).getBytes());
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

    }

主函数:

 1 package com.example.login;
 2 
 3 import java.util.Map;
 4 
 5 import com.example.login.service.LoginService;
 6 
 7 import android.os.Bundle;
 8 import android.app.Activity;
 9 import android.text.TextUtils;
10 import android.view.Menu;
11 import android.view.View;
12 import android.widget.CheckBox;
13 import android.widget.EditText;
14 import android.widget.Toast;
15 
16 public class MainActivity extends Activity {
17     private EditText et_username;
18     private EditText et_userpwd;
19     private CheckBox cb;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         et_username = (EditText) this.findViewById(R.id.et_username);
26         et_userpwd = (EditText) this.findViewById(R.id.et_userpwd);
27         cb = (CheckBox) this.findViewById(R.id.cb_remeber_pwd);
28         Map<String, String> map = LoginService.getSaveUserInfo(this);
29         if(map!=null){
30             et_username.setText(map.get("name"));
31             et_userpwd.setText(map.get("password"));
32         }
33     }
34 
35     // 登陆
36     public void login(View view) {
37         String name = et_username.getText().toString().trim();
38         String password = et_userpwd.getText().toString().trim();
39         if (TextUtils.isEmpty(name) || (TextUtils.isEmpty(password))) {
40             Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
41         } else {
42             // 登陆
43             if (cb.isChecked()) {
44                 // 保存密码
45                 boolean result = LoginService.saveUerInfo(this, name, password);
46                 if (result) {
47                     Toast.makeText(this, "保存信息成功", 0).show();
48                 }
49             }
50             // 登陆
51             if ("zhangsan".equals(name) && ("12345").equals(password)) {
52                 Toast.makeText(this, "登陆成功", 0).show();
53             } else {
54                 Toast.makeText(this, "登陆失败", 0).show();
55             }
56         }
57     }
58 
59     public EditText getEt_username() {
60         return et_username;
61     }
62 
63     public void setEt_username(EditText et_username) {
64         this.et_username = et_username;
65     }
66 
67     public EditText getEt_userpwd() {
68         return et_userpwd;
69     }
70 
71     public void setEt_userpwd(EditText et_userpwd) {
72         this.et_userpwd = et_userpwd;
73     }
74 
75     public CheckBox getCb() {
76         return cb;
77     }
78 
79     public void setCb(CheckBox cb) {
80         this.cb = cb;
81     }
82 
83 }

方法:

 1 package com.example.login.service;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 import java.util.HashMap;
11 import java.util.Map;
12 
13 import org.apache.http.entity.InputStreamEntity;
14 
15 import android.content.Context;
16 
17 public class LoginService {
18     public static boolean saveUerInfo(Context context, String UerName,
19             String password) {
20 
21         try {
22             File file = new File(context.getFilesDir(), "info.txt");
23             // File file = new File("/data/data/com.example.login/info.txt");
24             FileOutputStream fos = new FileOutputStream(file);
25             fos.write((UerName + "###" + password).getBytes());
26             return true;
27         } catch (Exception e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30             return false;
31         }
32 
33     }
34 
35     public static Map<String, String> getSaveUserInfo(Context context) {
36         try {
37             File file = new File(context.getFilesDir(), "info.txt");
38             FileInputStream fis = new FileInputStream(file);
39             BufferedReader br = new BufferedReader(new InputStreamReader(fis));
40             String str = br.readLine();
41             String[] infos = str.split("###");
42             Map<String, String> map = new HashMap<String, String>();
43             map.put("name", infos[0]);
44             map.put("password", infos[1]);
45             return map;
46         } catch (Exception e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50         return null;
51 
52 
53     }
54 }

 

android 中在内存中保存文件

标签:

原文地址:http://www.cnblogs.com/danwuxinbolg/p/4836723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!