采用内部存储的方式写入数据及读取数据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;
}
}
内部存储保存用户名和密码
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;
}
}
相关文章推荐
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- 数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- 数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)
- <NSCODING> 存储数据 NSUSERDEFAULTS 读取和写入
- 在VC下采用ADO实现BLOB(Binary)数据的存储,读取,修改,删除。
- 向文件中写入数据读取数据的几种方式
- Android学习笔记之数据的内部存储方式实习数据的读写、存储到Cache实现读写
- IPHONE数据存储的常用方式NSKEYEDARCHIVER/NSUSERDEFAULTS/PLIST WRITE写入
- HDFS的存储结构以及写入、读取hdfs数据操作流程简单总结
- Android 数据存储方式(一.文件+内部外部存储吐血详解)
- IPHONE数据存储的常用方式NSKEYEDARCHIVER/NSUSERDEFAULTS/PLIST WRITE写入
- 在VC下采用ADO实现BLOB(Binary)数据的存储,读取,修改,删除。
- 存储数据方法之一:NSUserDefaults 读取和写入
- 存储数据方法之一:NSUserDefaults 读取和写入