Android 中 使用 openFileOutput 和 openFileInput 存取数据
2018-03-22 15:18
411 查看
实现:
1、通过 openFileOutput 保存数据
2、通过 openFileOutput 读取数据
效果:
将数据存入
将数据取出
实现详情:
看了上面的效果图,相信布局应该没问题,而且如果是实际应用的话,还是要结合实际来做
1、通过 openFileOutput 保存数据
其核心代码为:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取输入的用户名密码
String names = name.getText().toString();
String pswds = pswd.getText().toString();
FileOutputStream fos = null;
try {
//获取文件输出流
//MODE_APPEND 检查文件是否存在,存在就往里面追加,不存在就创建一个文件
fos = openFileOutput("login",MODE_APPEND);
//保存用户名和密码
fos.write((names + " " + pswds).getBytes());
//清除缓存
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件输入流
try{
if(fos!=null)
fos.close();
} catch (IOException e){
e.printStackTrace();
}
}
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});注意存入时,是通过空格间隔的
2、通过 openFileOutput 读取数据
其核心代码为:
obtian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileInputStream fis = null;
byte[] buffer = null;
try {
//获取文件输入流
fis = openFileInput("login");
//定义保存数据的数组
buffer = new byte[fis.available()];
//从输入流中读取数据
fis.read(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(fis!=null)
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
//获取数组中的数据
String data = new String(buffer);
String names = data.split(" ")[0];
String pswds = data.split(" ")[1];
one.setText("账号是:" + names);
two.setText("密码是:" + pswds);
}
});这里用 split(" ") 方法实现数据分离(实际应用时可根据需求灵活运用)
源码:https://github.com/iscopy/Files
1、通过 openFileOutput 保存数据
2、通过 openFileOutput 读取数据
效果:
将数据存入
将数据取出
实现详情:
看了上面的效果图,相信布局应该没问题,而且如果是实际应用的话,还是要结合实际来做
1、通过 openFileOutput 保存数据
其核心代码为:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取输入的用户名密码
String names = name.getText().toString();
String pswds = pswd.getText().toString();
FileOutputStream fos = null;
try {
//获取文件输出流
//MODE_APPEND 检查文件是否存在,存在就往里面追加,不存在就创建一个文件
fos = openFileOutput("login",MODE_APPEND);
//保存用户名和密码
fos.write((names + " " + pswds).getBytes());
//清除缓存
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件输入流
try{
if(fos!=null)
fos.close();
} catch (IOException e){
e.printStackTrace();
}
}
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});注意存入时,是通过空格间隔的
2、通过 openFileOutput 读取数据
其核心代码为:
obtian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileInputStream fis = null;
byte[] buffer = null;
try {
//获取文件输入流
fis = openFileInput("login");
//定义保存数据的数组
buffer = new byte[fis.available()];
//从输入流中读取数据
fis.read(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(fis!=null)
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
//获取数组中的数据
String data = new String(buffer);
String names = data.split(" ")[0];
String pswds = data.split(" ")[1];
one.setText("账号是:" + names);
two.setText("密码是:" + pswds);
}
});这里用 split(" ") 方法实现数据分离(实际应用时可根据需求灵活运用)
源码:https://github.com/iscopy/Files
相关文章推荐
- Android---35---openFileInput、openFileOutput获取手机内存中的数据
- Android数据存储之文件 openFileOutput & openFileInput
- Android下openfileoutput和openfileinput的使用
- 使用openFileOutput和openFileInput读取文件
- 使用openFileInput和openfileOutput方法读写应用程序数据文件夹下的文件
- openFileOutput/openFileInput使用详解
- openFileInput()和 openFileOutput()方法利用java流将数据存储到文件中
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput() .
- Android openFileOutput 和 openFileInput
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android:内部存储空间 IO 操作openFileInput()、output(13)
- Android--模拟登陆用户名密码,使用File或openFileOutput保存文件
- 内部存储之 openfileinput openfileoutput使用
- Android文件操作中的openFileOutPut和openFileInput
- andriod 内存数据读取 写入操作(openFileInput,openFileOutput)
- Android openFileOutput 和 openFileInput
- Android学习(18) -- 数据存储之openFileOutput
- Android文件操作中的openFileOutPut和openFileInput
- Androidx学习笔记(15)-- 数据存储之openFileOutput()
- openFileOutput() openFileInput() 对数据进行读取和存储