Android File文件存储功能
1.介绍
2.使用方法
3.文件存储位置
4.java后台代码
package com.lucky.test47file; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream; public class MainActivity extends AppCompatActivity {
EditText editText1;
TextView textView2;
Button button1;
Button button2;
String fileName="lucky2.txt"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=findViewById(R.id.editText);
textView2=findViewById(R.id.textView2);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//采用打印流,向文本写入数据,参数1为文件名称,参数2为数据的添加模式
FileOutputStream fileOutputStream=openFileOutput(fileName,MODE_APPEND);//实例化文件输出流
PrintStream printStream=new PrintStream(fileOutputStream); //实例化打印流
printStream.println(editText1.getText().toString()); //使用打印流输出数据
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//打开文件,读取文本数据
FileInputStream fileInputStream=openFileInput(fileName); //实例化文件输入流
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
String tempString;
textView2.setText("文件的路径为:"+MainActivity.this.getFilesDir());
while ((tempString=bufferedReader.readLine())!=null){ //逐行读取数据,直到结尾
textView2.append("\n"+tempString); //采用添加的方式保存文本
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}); }
}
5.文件的保存位置查看方法
(1)点击右侧的 Device File Explorer
(2)找到文件夹
data--->data--->com.lucky.test47file
6.效果图
对应项目名为:test47
Android File文件存储功能的更多相关文章
- Android开发--数据存储之File文件存储
转载来自:http://blog.csdn.net/ahuier/article/details/10364757,并进行扩充 引言:Android开发中的数据存储方式 Android提供了5种方式存 ...
- Android学习——文件存储
在Andriod开发中,文件存储和Java的文件存储类似.但需要注意的是,为了防止产生碎片垃圾,在创建文件时,要尽量使用系统给出的函数进行创建,这样当APP被卸载后,系统可以将这些文件统一删除掉.获取 ...
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- Android使用文件存储数据
Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...
- 19.Android之文件存储方法学习
Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...
- File文件存储
文件存储的核心是Context提供了一个openFileOutput()与openFileInput()俩个方法 课程demo public class MainActivity extends Ap ...
- android File文件的读写操作
本程序实现两个文本框和两个按钮,其中一个文本框和一个按钮用于读取文件内容.另一个文本框和另一个按钮用于写入. java代码: package com.example.activity; import ...
- android之文件存储和读取
一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...
- Android - 读取文件存储的数据
存取手机中的文件数据. 写入和读取的操作格式均为UTF-8. import java.io.File; import java.io.FileInputStream; import java.io.F ...
随机推荐
- elasticsearch配置小记(转)
原文 http://bigbo.github.io/pages/2015/04/10/elasticsearch_config/ 基于 elasticsearch 1.4.4 版本.安装方式为RPM ...
- std:: lower_bound std:: upper_bound
std:: lower_bound 该函数返回范围内第一个不小于(大于或等于)指定val的值.如果序列中的值都小于val,则返回last.序列应该已经有序! eg: #include <iost ...
- 关于getchar的一些思考
这个问题是有一段代码引起的: 代码1: #include<iostream> using namespace std; int main() { char t; t=getchar(); ...
- 414. Third Maximum Number数组中第三大的数字
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- 为Vmware里安装的CentOS7.5设置静态IP
[引言]为测试搭建大数据集群环境,采用在Vmware里安装了几台CentOS7.5的虚拟机,在测试过程中,出现启动虚拟机后,虚拟机的IP地址会变,不方便测试集群,所以需要设置静态IP,在此,记录我的设 ...
- python字符串大小写转换
str = "www.w3cSChool.cn"print(str.upper()) # 把所有字符中的小写字母转换成大写字母print(str.lower()) # 把所有字符中 ...
- su 和sudo su 的区别
su "user" 执行该命令,需要输入password,它是"user"中定义的用户的password,即,要变换成的用户的password.(如果已经用ro ...
- [转]Replace all UUIDs in an ATL COM DLL.
1. Introduction. 1.1 Recently, a friend asked me for advise on a very unusual requirement. 1.2 He ne ...
- The 'microsoft.jet.oledb.4.0' provider is not registered on the local machin
1,2选取目标站点,然后3的高级设置,4启用32位的应用程序的属性变为true就可以了.当然,网络上还有其他的版本,你也可以尝试下. 原文地址:http://weblogs.asp.net/kenco ...