Android开发之文件存储
来源:互联网 发布:js怎么给元素添加属性 编辑:程序博客网 时间:2024/05/28 23:21
Android系统实现数据持久化有三种,1,文件存储,2,SharedPreference,3,数据库存储
将数据存储到文件中,4.2之后有两个参数,一个是覆盖,一个是追加
MODE_PRIVATE MODE_APPEND
界面布局
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Type something here" /></RelativeLayout>
主活动代码
package com.example.filepersistencetest;import java.io.BufferedWriter;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Menu;import android.widget.EditText;public class MainActivity extends Activity { private EditText edit; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit=(EditText)findViewById(R.id.edit); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } protected void onDestroy(){ super.onDestroy(); String inputText=edit.getText().toString(); save(inputText); } public void save(String inputText) { FileOutputStream out=null; BufferedWriter writer=null; try{ out=openFileOutput("data",Context.MODE_PRIVATE); writer=new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); }catch(IOException e) { e.printStackTrace(); }finally{ try{ if(writer!=null) {writer.close();} }catch(IOException e){ e.printStackTrace(); } } }}
0 0
- Android开发之文件存储
- Android开发之-------文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- Android开发之资源文件存储
- 数据压缩与信息熵
- 运行此安装程序之前,必须安装 32 位 Windows 映像处理组件(WIC)
- 一文看懂大数据的技术生态圈,Hadoop,hive,spark都有了
- The Tomcat server configuration at \Servers\Tomcat v6.0 Server at localhost-config is missing
- Android修改AlertDialog的背景颜色
- Android开发之文件存储
- Xposed源码剖析——Xposed初始化
- ajaxFileUpload实现图片的上传并预览
- 产品学习笔记1—所谓产品汪
- [pat]1025. PAT Ranking (25)
- QT中各种MessageBox的使用
- ArcGIS Engine代码共享-工作空间(workspace)
- 彻底停止运行线程池ThreadPoolExecutor
- which whereis locate find