• android 中在内存中保存文件


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

    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 }
  • 相关阅读:
    大数据学习之Scala数组和集合学习38
    大数据学习之Scala语言函数学习37
    大数据学习之Scala语言基本语法学习36
    大数据学习之Storm实时统计网站访问量案例35
    大数据学习之storm-wordcount 实时版开发以及分组策略34
    大数据学习之Storm实时计算概述及安装部署33
    Java中级学习6多线程之线程池
    大数据学习之kafka的 Java API操作32
    大数据学习之Kafka消息队列31
    【代码加密】安装PHP源码加密模块
  • 原文地址:https://www.cnblogs.com/danwuxinbolg/p/4836723.html
Copyright © 2020-2023  润新知