您的位置:首页 > 移动开发 > Android开发

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

2016-09-01 20:17 309 查看
前言:昨天学习了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;
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 存储 io流
相关文章推荐