采用内部存储的方式写入数据及读取数据xs

前言:昨天学习了java的IO流后,对这种字节方式的读取就觉得比较有趣,今天就学习了android的内部文件存储方式,目前学的比较简单,只能写入和读取一些字符数据,下面写了一个简单的小demo,用来保存用户输入的用户名和密码,个人觉得这种写法虽然不完美,但是这种写法却能够重复使用,确实比较神奇,233333

内部存储保存用户名和密码

1.下面是布局文件,定义了两个TextView用来显示用户名和密码,两个EditText来获取用户输入的内容,两个TextView来用于显示得到的数据

<?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:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>

        <EditText
            android:id="@+id/usernameET"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>

        <EditText
            android:id="@+id/passwordET"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>

    </LinearLayout>
    <Button
        android:id="@+id/loginBT"
        android:text="登陆"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>
2.这个是MainActivity.java文件,其实我觉得可以把下面的写入方法和读取方法封装成一个类,毕竟用的都是一样的代码

package com.example.nete.myapplication;

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.TextView;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText usernameET;
    private EditText passwordET;
    private Button loginBT;
    private TextView nameTV;
    private TextView passTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化需要使用到的控件
        loginBT = (Button) findViewById(R.id.loginBT);
        passwordET = (EditText) findViewById(R.id.passwordET);
        usernameET = (EditText) findViewById(R.id.usernameET);
        nameTV = (TextView) findViewById(R.id.name);
        passTV = (TextView) findViewById(R.id.pass);
        loginBT.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        //最后获取输出框字符串
        WriteFile(usernameET.getText().toString());
        nameTV.setText("用户名:" + ReadFile());
        WriteFile(passwordET.getText().toString());
        passTV.setText("密 码:" + ReadFile());
    }


    public void WriteFile(String content) {
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("login.txt", MODE_PRIVATE);
            fos.write(content.getBytes());//写入获得到的用户名密码
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public String ReadFile() {

        //新建两个局部变量用于保存读取到的数据
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        String content = null;
        byte[] buffer = null;
        try {

            fis = openFileInput("login.txt");//获取文件输入流
            buffer = new byte[1000];//定义一次读取的字节数,不要太大
            baos = new ByteArrayOutputStream();//定义用于存放数据的写入内容
            int len = 0;
            //添加一个循环语句,当len==-1,就说明此时的数据已经读取完毕
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            content = baos.toString(); //将获取到的数据传到content
            fis.close();//关闭文件输入流
            baos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return content;
    }


}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值