• Files存储练习


    package com.hanqi.myapplication;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    public class Main2Activity extends AppCompatActivity {
        EditText user_name;
        EditText user_password;
        Button bt_1;
        Button bt_2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            user_name=(EditText)findViewById(R.id.user_name);
            user_password=(EditText)findViewById(R.id.user_password);
            bt_1=(Button)findViewById(R.id.bt_1);
            bt_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String input_username=user_name.getText().toString();
                    String input_password=user_password.getText().toString();
                    if (input_username.trim().length()==0||input_password.trim().length()==0)
                    {
                        Toast.makeText(Main2Activity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    }else
                    {
                        Intent intent=new Intent(Main2Activity.this,Main22Activity.class);
                        startActivity(intent);
                        try
                        {
                            File file=getFilesDir();
                            String path=file.getAbsolutePath();
                            FileOutputStream fos=openFileOutput("test.txt", MODE_APPEND);
                            PrintStream ps=new PrintStream(fos);
                            ps.print(input_username);
                            ps.close();
                            fos.close();
                            FileOutputStream fos1=openFileOutput("tes.txt", MODE_APPEND);
                            PrintStream ps1=new PrintStream(fos1);
                            ps1.print(input_password);
                            ps1.close();
                            fos1.close();
                            Toast.makeText(Main2Activity.this, "保存成功", Toast.LENGTH_SHORT).show();
                        }catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }
    package com.hanqi.myapplication;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.EditText;
    
    import java.io.FileInputStream;
    
    public class Main22Activity extends AppCompatActivity {
        EditText file_name;
        EditText file_password;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main22);
            file_name=(EditText)findViewById(R.id.file_name);
            file_password=(EditText)findViewById(R.id.file_password);
            try
            {
                FileInputStream fis =openFileInput("test.txt");
                byte[] bytes =new byte[1024];
                int i=0;
                String str1 = "";
                while ((i=fis.read(bytes))>0)
                {
                    String str = new String(bytes,0,i);
                    str1+=str;
                }
                fis.close();
                file_name.setText(str1);
    
                int ii=0; FileInputStream fis1 =openFileInput("tes.txt");
                byte[] bytes1 =new byte[1024];
                String str2 = "";
                while ((ii=fis1.read(bytes1))>0)
                {
                    String str = new String(bytes1,0,ii);
                    str2+=str;
                }
                fis1.close();
                file_password.setText(str2);
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <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="com.hanqi.myapplication.Main2Activity"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/user_name"
            android:hint="用户名"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/user_password"
            android:hint="用户密码"/>
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/bt_1"
                android:text="登陆"
                />
    
    
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <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="com.hanqi.myapplication.Main22Activity"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名:"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/file_name"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户密码:"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/file_password"/>
        </LinearLayout>
    
    
    </LinearLayout>

  • 相关阅读:
    2020-09-05:虚拟内存知道么?什么时候使用虚拟内存?虚拟内存除了扩大内存还有什么用?
    2020-09-04:函数调用约定了解么?
    2020-09-03:裸写算法:回形矩阵遍历。
    2020-09-02:Sqoop的工作原理?
    2020-09-01:mysql里什么是检查点、保存点和中间点?
    2020-08-31:描述HTTP的版本之间的区别,主要是1.0/1.1/2.0三个版本的区别。
    2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
    2020-08-29:进程线程的区别,除了包含关系之外的一些区别,底层详细信息?
    2020-08-28:边缘网关协议了解么?简单描述一下。
    2020-08-27:OpenStack与Docker的区别?
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5533724.html
Copyright © 2020-2023  润新知