Android文件存储学习
来源:互联网 发布:基恩士视觉软件 编辑:程序博客网 时间:2024/05/16 14:57
一、将数据存储到文件中
Context类中提供了一个openFileOutput()方法,可以将数据存储到指定的文件中。该方法接收两个参数,第一个参数是文件名,在文件创建的时候使用这个名字(默认存储到/data/data/package name/files/目录下),第二个参数是文件的操作模式,主要有MODE_PRIVATE和MODE_APPEND.
在activity_main.xml中的代码如下:
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="type sth. here!" /></RelativeLayout>
MainActivity代码:
public class MainActivity extends Activity {private EditText et;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et = (EditText) findViewById(R.id.edit);}@Overrideprotected void onDestroy() {super.onDestroy();String inputText = et.getText().toString();save(inputText);}private void save(String inputText) {FileOutputStream fs = null;BufferedWriter bw = null;try {fs = openFileOutput("data_cat", Context.MODE_PRIVATE);bw = new BufferedWriter(new OutputStreamWriter(fs));bw.write(inputText);} catch (Exception e) {// TODO: handle exception} finally {{try {if (bw != null)bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}
0 0
- Android文件存储学习
- Android 文件存储学习
- Android学习之 文件存储
- Android学习之文件存储
- Android学习笔记--文件存储数据
- Android学习之文件存储方式
- Android学习笔记——文件存储
- Android存储-文件存储
- android存储---文件存储
- Android学习14——数据存储之文件存储
- Android学习 数据存储之_文件存储
- android数据存储中的文件存储学习记录
- Android 存储学习之在内部存储中读写文件
- Android 存储学习之在外部存储中读写文件
- android学习十(android的文件存储)
- Android-文件存储-文本存储
- Android 数据存储--文件存储
- android 数据存储-文件存储
- hdu1007最近点对(分治)
- 仿映客送礼特效
- 非对称加密
- [React Native]获取网络状态
- JNI在编译时的源文件和生成文件的层次结构详解
- Android文件存储学习
- 当使用dbeaver导出多个sql文件,怎么快速的把它合并成一个文件,可以针对多种不同的类型
- 源代码加密解决方案(透明加密与环境加密的区别 )
- 文章标题
- Java数据类型和运算符:
- 树莓派+motion 搭建摄像头监控系统
- BZOJ1911 Apio2010 特别行动队
- @ResponseBody返回string自动增加“”
- 使用HttpUrlConnection或者HttpClient做https请求时导致的SSLHandshakeException异常