zoukankan      html  css  js  c++  java
  • Android将数据存储到应用的数据目录下

    下面是具体代码,其中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,右上角有导出

    导出到桌面,进行查看

  • 相关阅读:
    webug第四关:告诉你了flang是5位数
    webug第三关:你看到了什么?
    webug第二关:从图片中你能找到什么?
    webug第一关:很简单的一个注入
    redhat-DHCP服务的配置与应用
    python-基础入门-7基础
    bWAPP----SQL Injection (GET/Search)
    ctf-web-sql
    光棍节程序员闯关秀writeup
    ajax回调函数Status问题
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/12246435.html
Copyright © 2011-2022 走看看