以android登录案例来介绍文件的读取与androidAPI给予的方法

第一步:绘制界面

  绘制方法:在线性布局下面设置相对布局

  代码部分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical"
     >

<EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        />
    <EditText
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入密码"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住用户名和密码"
            android:layout_centerVertical="true"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="登录"
            android:layout_alignParentRight="true"
            android:onClick="login"
            />
    </RelativeLayout>
</LinearLayout>

第二步:底层逻辑

public class MainActivity extends Activity {

private EditText et_name;//用于保存用户输入的账号
    private EditText et_pass;//用于保存用户输入的密码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //获取用户保存输入的账号及密码信息
        et_name = (EditText) findViewById(R.id.et_name);
        et_pass = (EditText) findViewById(R.id.et_pass);

  //界面数据回显
        readAccount();
        
    }

public void readAccount(){

  //path=“data/data/com.itheima.rwinrom/info.txt”表示info.txt在android中的路径
        File file = new File("data/data/com.itheima.rwinrom/info.txt");

  //如果该文件存在则执行回显的功能代码        

  if(file.exists()){
            try {
                FileInputStream fis = new FileInputStream(file);
                //把字节流转换成字符流
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                //读取txt文件里的用户名和密码
                String text = br.readLine();
                String[] s = text.split("##");
                
                et_name.setText(s[0]);
                et_pass.setText(s[1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

public void login(View v){
        //获取输入框中的数据,并且保存到相应的变量中
        String name = et_name.getText().toString();
        String pass = et_pass.getText().toString();
        
        CheckBox cb = (CheckBox) findViewById(R.id.cb);
        //判断选框是否被勾选
        if(cb.isChecked()){
            //data/data/com.itheima.rwinrom:这就是内部存储空间的路径 , com.itheima.rwinrom为项目的包名
            File file = new File("data/data/com.itheima.rwinrom/info.txt");
            FileOutputStream fos;  //写入文件使用FileOutStream进行写入数据到文件中
            try {
                fos = new FileOutputStream(file);
                fos.write((name + "##" + pass).getBytes());  //将数据以字节方式写入
                fos.close(); //关流
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        //创建并显示吐司对话框
        Toast.makeText(this, "登录成功", 0).show();
    }
    
}
第三步:总结案例步骤

 第一步:定义对的布局文件

 第二步:拿到用户输入的数据,并且判断用户是否点击记住密码的功能

 第三步:如果开启记住密码功能则将用户的数据进行保存到info.txt文件中

 第四步:实现数据回显功能:(1)判断文件是否存在,如果存在则打来文件输入流        读取数据,入股读取到了数据则将数据进行显示到输入框中

第四步:补充

*getFilesDir()得到的file对象的路径是data/data/com.itheima.rwinrom2/files
    * 存放在这个路径下的文件,只要你不删,它就一直在
* getCacheDir()得到的file对象的路径是data/data/com.itheima.rwinrom2/cache
    * 存放在这个路径下的文件,当内存不足时,有可能被删除
* 系统管理应用界面的清除缓存,会清除cache文件夹下的东西,清除数据,会清除整个包名目录下的东西

Android first---文件读取(登录案例编写为主)的更多相关文章

  1. 8.Android-简单的登录案例编写

    本章来学习登录案例,由于还未学习自定义控件外观,所以ui界面先用最简单的,并保存登录账号密码到data/data/包名/files下 1.学习之前需要掌握的Context类(通过Context来往AP ...

  2. Android的文件读取与存储

    Java新建文件,然后就可以写入数据了,但是Android却不一样,因为Android是 基于Linux的,我们在读写文件的时候,还需加上文件的操作模式 Environment类是一个提供访问环境变量 ...

  3. android 内部文件读取

    Android 文件管理方法 Android使用的是基于Linux的文件系统,对于文件的訪问和管理是通过权限设置来限制的. 在Linux系统中,文件权限分别描写叙述了创建者.同组用户和其它用户对文件的 ...

  4. 二十、Android -- SDcard文件读取和保存

    背景                                                                                            一些东西可以 ...

  5. Android -- SDcard文件读取和保存

    背景                                                                                            一些东西可以 ...

  6. Android MVP框架实现登录案例

    一.Model package com.czhappy.mvpdemo.model; /** * author: chenzheng * created on: 2019/5/16 11:06 * d ...

  7. Android(java)学习笔记213:开源框架post和get方式提交数据(qq登录案例)

    1.前面提到Http的get/post方式  . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2.Android应用会经常使用http协议进行传输,网上会有很完善 ...

  8. Android(java)学习笔记212:中文乱码的问题处理(qq登录案例)

    1.我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed&q ...

  9. Android(java)学习笔记156:开源框架post和get方式提交数据(qq登录案例)

    1. 前面提到Http的get/post方式  . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2. Android应用会经常使用http协议进行传输,网上会有很 ...

随机推荐

  1. Excel和datatable相互操作

    /// <summary> /// Excel文档 /// </summary> /// <param name="table"></pa ...

  2. js的extend和fn.extend使用

    $.fn.extend扩展的是一个jQuery对象函数,$.extend扩展的是一个jQuery全局函数 <!DOCTYPE html> <html> <head> ...

  3. eclipse.ini

    -startup plugins/org.eclipse.equinox.launcher_1..jar --launcher.library plugins/org.eclipse.equinox. ...

  4. uilmit 优化

    #!/bin/bash sed -i "/^ulimit -SHn.*/d" /etc/rc.local echo "ulimit -SHn 102400" & ...

  5. apache本地域名ip重定向vhosts

    apache本地域名ip重定向,使本机通过指定域名访问到指定ip路径. 1.apache配置apache/conf/httpd.conf  : 开启配置 Include conf/extra/http ...

  6. Android Studio 环境部署 (转载)

    Android Studio的安装和使用过程经常需要下载以来文件和Gradle版本,而Google网站在天朝的访问可谓步履维艰,没有稳定的FQ工具是非常痛苦的.何况,作为一个优秀的程序员,不能访问国外 ...

  7. Rails problem

    总是wa~ #include <stdio.h> int main() { ]; ], b[]; while(scanf("%d %s %s", &n, a, ...

  8. Final-阶段站立会议4

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  9. mongodb 安装后 出现警告:** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

    警告问题:当前mongodb 支持的最大文件数有256个,但是推荐至少1024个. 解决办法: 1.关闭现在打开的mongodb 终端窗口 2.重新打开终端并运行一下命令: sudo launchct ...

  10. shell读取文件每行,并执行命令

    #!/bin/bash while read line do $line & done < /path/filename