下面是具体代码,其中MainActivity.java的部分代码有修改,在文章后面给出

logindemo_layout.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background1"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:padding="30dp"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_marginLeft="30dp"
android:drawableLeft="@mipmap/ic_launcher_round"
android:text="家庭记账本"
android:textSize="40sp"
android:layout_height="wrap_content"/> <EditText
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:id="@+id/et_username"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:hint="密码" />
<Button
android:layout_width="match_parent"
android:text="登录"
android:textSize="20sp"
android:id="@+id/bt_login"
android:layout_height="wrap_content"/> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="没有账号,立即去注册"
android:textColor="#00ffff"
android:textSize="16sp" />
</RelativeLayout> </LinearLayout> </RelativeLayout>

MainActivity.java

  package com.example.logindemo;

import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { private static final String TAG ="MainActivity";
private TextView mUsername;
private TextView mPassword;
private Button mLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logindemo_layout);
//第一步,找到控件
initViews();
//第二步,给我们的登录按钮设置监听事件
initListener();
} /**
* 这个方法,我们用来找对应的控件
*/
private void initViews(){
mUsername= (TextView)this.findViewById(R.id.et_username);
mPassword= (TextView)this.findViewById(R.id.et_password);
mLogin = (Button)this.findViewById(R.id.bt_login);
}
/**
* 这个方法就是给登录按钮设置点击的监听
*/
private void initListener(){
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"点击了登录按钮");
handlerLoginEvent(v);
}
});
} /**
* 处理登录事件
* @param v
*/
private void handlerLoginEvent(View v) {
//第三部,我们要拿到界面上的信息,包括账号和密码
//账号
String usernameText =mUsername.getText().toString();
//密码
String passwordText =mPassword.getText().toString();
//把账号和密码保存起来
saveUserInfo(usernameText,passwordText);
}
private void saveUserInfo(String usernameText,String passwordText){
Log.d(TAG,"保存用户信息");
File file =new File("info.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
//以特定的格式存储
fileOutputStream.write((usernameText+"***"+passwordText).getBytes());
fileOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}

点击登录后,发现程序崩了

为什么我们直接写一个文件名的时候,去写文件,报出的异常是read-only。
在安卓系统中,每一个应用就相当于一个用户,每个用户(应用)的权限是特定的,不能够操作其它应用的内容

找到我们的项目

查看我们的info文件应该存放的目录

修改其中saveUserInfo方法

private void saveUserInfo(String usernameText,String passwordText){
Log.d(TAG,"保存用户信息");
try {
File file =new File("/data/data/com.example.logindemo/info.txt");
if(file.exists()){
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
//以特定的格式存储
fileOutputStream.write((usernameText+"***"+passwordText).getBytes());
fileOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}

点击登录

查看发现info.txt文件,且内容为zzw***123说明保存成功。

还有另一种方法

通过Android Devices Monitor查看

https://www.cnblogs.com/rivers-enduring/p/9212111.html

打开后,在File Explorer下找到包/data/data/com.example.logindemo/

选中info.txt,右上角有导出

导出到桌面,进行查看

Android将数据存储到应用的数据目录下的更多相关文章

  1. Android中数据存储(一)

    国庆没有给国家添堵,没有勾搭妹子,乖乖的写着自己的博客..... 本文将为大家介绍Android中数据存储的五种方式,数据存储可是非常重要的知识哦. 一,文件存储数据 ①在ROM存储数据 关于在ROM ...

  2. Android本地数据存储复习

    Android本地数据存储复习 Android无论是应用层还是系统层都需要在本地保存一些数据,尤其在应用层中使用的就更为普遍,大体有这么几种:SharedPreference,file,sqlite数 ...

  3. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  4. Android实现数据存储技术

    转载:Android实现数据存储技术 本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用Shar ...

  5. android中数据存储

    android中数据存储     Android 中存储数据的方式有五种:SQLite数据库.文件存储.内容提供者.网络.SharedPreferences(Key----value)五种存储方式. ...

  6. Android中数据存储(四)——ContentProvider存储数据

    目录(?)[+]   当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方 ...

  7. Android中数据存储(三)——SQLite数据库存储数据

    当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方式: 1. Share ...

  8. 关于Android开发数据存储的方式(一)

    关于Android开发数据存储方式(一) 在厦门做Android开发也有两个月了,快情人节了.我还在弄代码. 在微信平台上开发自己的APP,用到了数据存储的知识,如今总结一下: 整体的来讲.数据存储方 ...

  9. Android本地数据存储: ASimpleCache

    一:前言 在上一篇博客Android本地数据存储: Reservoir 博客中,我提到,除了Reservoir库,还可以采用ASimpleCache开源库,来实现本地数据存储.昨天并没有仔细的对比Re ...

随机推荐

  1. put、patch与post区别

    idempotent 幂等的 如果一个方法重复执行多次,产生的效果是一样的,那就是idempotent的:  idempotent的意思是如果相同的操作再執行第二遍第三遍,結果還是一樣. POST方法 ...

  2. 拼接 字典序min

    给定一个字符串类型的数组strs,找到一种拼接方式,使得把所有字符串拼起来之后形成的字符串具有最低的字典序. 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个 ...

  3. 一个包含arctan与arctanh的积分

    \[\Large\int_0^1\frac{\arctan x \,\operatorname{arctanh} x\, \ln x}{x}\mathrm{d}x=\frac{\pi^2}{16}\m ...

  4. Cocos纹理理解

    原文:https://blog.csdn.net/u010223072/article/details/78287294 理论要点 要点一: 文件格式与像素格式的区别:文件格式是图像为了存储信息而使用 ...

  5. tomcat8配置了tomcat-users.xml,报403 Access Denied

    配置了tomcat-users.xml之后,重启tomcat服务,仍然访问拒绝. 原因:tomcat8.5 更改之后,仍然访问拒绝. 还需步骤如下: vi /usr/local/tomcat/apac ...

  6. VB.NET中Sub和Function的区别

    function是函数,sub是子程序,都可以传递参数,但函数有返回值,子程序没有 function 可以用自身名字返回一个值,sub 需定义别的变量,用传址方式传回值. Sub 过程与Functio ...

  7. [转]利用 Commons-Fileupload 实现文件上传

    转载 Java Web开发人员可以使用Apache文件上传组件来接收浏览器上传的文件,该组件由多个类共同组成,但是,对于使用该组件来编写文件上传功能的Java Web开发人员来说,只需要了解和使用其中 ...

  8. gensim加载词向量文件

    # -*- coding: utf-8 -*- # author: huihui # date: 2020/1/31 7:58 下午 ''' 根据语料训练词向量,并保存向量文件 ''' import ...

  9. FiBiNET-学习

    Our main contributions are listed as follows: • Inspired by the success of SENET in the computer vis ...

  10. 修改vue中的挂载页面(index.html)的路径

    修改vue中的挂载页面(index.html)的路径 2019年03月30日 12:07:12 VegasLemon 阅读数 501    版权声明:本文为博主原创文章,未经博主允许不得转载. htt ...