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 ...
随机推荐
- DLL卸载
[DLL卸载] 1.扫描Module.通过CreateToohelp32Snapshot.Module32First.Module32Next来完成. 2.通过FreeLibrary来卸载.通过在ke ...
- 安装 Windows Service
1.打开 VS 命令行窗口 2. installutil /u service文件路径 (卸载原有服务) 3, installutil /i service 文件路径 (安装服务)
- linux tcpdump补充
If they are going across the loopback interface, you may have to tell tcpdump to read that interface ...
- XML数据格式简介
---------------siwuxie095 XML 简介 XML,即 可扩展标记语言(Extensible Markcup La ...
- 447. Number of Boomerangs 回力镖数组的数量
[抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- 125. Valid Palindrome判断有效的有符号的回文串
[抄题]: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- MRPT - Mobile Robot Programming Toolkit
1. https://www.mrpt.org/Building_and_Installing_Instructions#1_Prerequisites P1. error C2371: “int32 ...
- if else的执行流程
int main(void) { int a, b; char op; float ans; scanf_s("%d%c%d",&a,&op,1,&b); ...
- GPS通讯协议协议(NMEA0183)
一.简介 GPS(全球定位系统)接收机与手持机之间的数据交换格式一般都由生产厂商缺省定制,其定义内容普通用户很难知晓,且不同品牌.不同型号的GPS接收机所配置的控制应用程序也因生产厂家的不同而不同.所 ...
- keys()
keys():返回一个数组,里面是符合匹配模式的键列表 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $pattern = ...