下面是具体代码,其中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将数据存储到应用的数据目录下全部内容,希望文章能够帮你解决Android将数据存储到应用的数据目录下所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)