与前面的文章http://leequer.iteye.com/blog/607079android的数据存储和访问 附源码这篇文章相比,添加了文件的读取功能和代码的优化
package cn.lee.data;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import cn.lee.Manager.DataManager;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class AboutDateActivity extends Activity {private EditText fileNameEditText;private EditText fileContentEditText;private EditText readfileContentEditText;private Button button;private Button readButton;private static final String TAGSTRING = "AboutDateActivity";/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button                  = (Button) this.findViewById(R.id.button);readButton              = (Button) this.findViewById(R.id.readButton);fileNameEditText        = (EditText) this.findViewById(R.id.fileName);fileContentEditText     = (EditText) this.findViewById(R.id.fileContent);readfileContentEditText = (EditText) this.findViewById(R.id.readfileContent);// 读取readButton.setOnClickListener(onClickListener);// 保存button.setOnClickListener(onClickListener);}/** * 由于这个内部实现类是一个接口的实例 所以这里把他提取出来 作为一个成员变量 2010-04-18 */private View.OnClickListener onClickListener = new View.OnClickListener() {/** * 由于button的父类是view 所以这里的onclick的参数传来的view v就可以找到这个button */public void onClick(View v) {Button button = (Button) v;// 将父类view强转成子类buttonint info = R.string.sus;String fileNameString = fileNameEditText.getText().toString();if ("".equals(fileNameString)) {info = R.string.filenamenotnull;} else {FileOutputStream fileOutputStream = null;FileInputStream  fileInputStream  = null;try {switch (button.getId()) {case R.id.button: {// 保存String fileContentString = fileContentEditText.getText().toString();fileOutputStream = AboutDateActivity.this.openFileOutput(fileNameString,Context.MODE_PRIVATE);DataManager.saveDate(fileOutputStream,fileContentString);break;}case R.id.readButton: {fileInputStream = null;fileInputStream = AboutDateActivity.this.openFileInput(fileNameString);String contentString = DataManager.readDate(fileInputStream);readfileContentEditText.setText(contentString);break;}default:break;}} catch (Exception e) {// TODO Auto-generated catch blockLog.i(TAGSTRING, e.toString());info = R.string.infor;} finally {try {if(fileOutputStream!=null){fileOutputStream.close();}if(fileInputStream !=null){fileInputStream.close();}} catch (IOException e) {Log.i(TAGSTRING, e.toString());info = R.string.infor;}}}Toast.makeText(AboutDateActivity.this, info, 1).show();}};}

更多相关文章

  1. Android(安卓)NDK 知识系列(四)
  2. PC安卓模拟器PANIC: Could not open:C:\Documents and Settings
  3. SDL2库(4)-Android(安卓)端源码简要分析(AudioSubSystem)
  4. Android(安卓)rom开发:app运行异常,报libc: Fatal signal 11 (SIGS
  5. MAC 配置android adb命令的环境变量
  6. Android(安卓)Vold和SDIO冲突问题解决方法
  7. [RK3288][Android6.0] 调试笔记 --- user版本默认显示开发者选项
  8. Android入门教程2【开发环境搭建】
  9. 二.系统编译--Ubuntu14.04上搭建android源码编译环境

随机推荐

  1. android应用程序的数据存放目录
  2. Android属性动画---Property Animation(三
  3. Android给apk签名
  4. APK 变成 ZIP:Android(安卓)手机应用程序
  5. Android(安卓)网络编程——HTTP请求的封
  6. android 使用SharedPreferences 实现登录
  7. AndroidManifest.xml中application name
  8. Android(安卓)UI设计——ViewPage中自定
  9. Android中文件与文件夹的创建(file)
  10. Android开机动画之开机bootvideo与bootan