现在的位置: 首页 > 综合 > 正文

Android开发循序渐进实例3–文件读写例子

2012年06月11日 ⁄ 综合 ⁄ 共 3380字 ⁄ 字号 评论关闭

在此实例中,重点展示使用Android平台提供的SharedPreferences存储、自定义文件存储二种方式。

1. 按照如下的配置以及在开发循序渐进实例1中描述的方法创建整个项目Base:
 project Name: ExampleThree
 Platform: Android2.0;
 Application name: ExampleThree
 package name: com.example
 Activity: MainActivity
 Resource file: main.xml
 main.xml有二个TextView:
 Id: @+id/TextView01
        Text: UserName:
 Id: @+id/TextView02
        Text: Password:
 main.xml 有二个EditView:
 Id:@+id/EditUserName
 Text: blank
 Layout width: fill_parent
 Id:@+id/EditPassword
 Text:blank
 Password: true
 Layout width: fill_parent
 main.xml 有二个Button:
 Id:  @+id/SaveByPref
 Text: Save by preference
 Id:  @+id/SaveToFile
 Text: Save to self-defined file

2. 在MainActivity中添加如下的代码:
常量定义部分:
 public static final String SECTION_NAME = "ExampleThree";
 public static final String USERNAME = "UserName";
 public static final String PASSWORD = "Password";
 private static final String FILE_NAME = "UserInfo.pwd";
模块函数部分:
 private void load_values_from_file() {
  String path = this.getFilesDir().getAbsolutePath() + File.separator + FILE_NAME;
  File file = new File(path);
  if (file.exists()) {
   show_file_values();
  }
 }

 private void show_file_values() {
  FileInputStream fis = null;
  DataInputStream dis = null;

  try {
   fis = openFileInput(FILE_NAME);
   dis = new DataInputStream(fis);
   String strUserName = dis.readUTF();
   String strPassword = dis.readUTF();
   EditText evUserName = (EditText) findViewById(R.id.EditUserName);
   EditText evPassword = (EditText) findViewById(R.id.EditPassword);
   evUserName.setText(strUserName);
   evPassword.setText(strPassword);
  } catch (IOException ioe) {
   ioe.printStackTrace();
  } finally {
   if (dis != null) {
    try {
     dis.close();
    } catch (IOException ioe) {
    }
   }
  }
 }

 private void attach_button_events() {
  Button buttonSaveByPref = (Button) findViewById(R.id.SaveByPref);
  buttonSaveByPref.setOnClickListener(saveByPref_listener);

  Button buttonSaveToFile = (Button) findViewById(R.id.SaveToFile);
  buttonSaveToFile.setOnClickListener(saveToFile_listener);
 }

 private Button.OnClickListener saveByPref_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   EditText evUserName = (EditText) findViewById(R.id.EditUserName);
   EditText evPassword = (EditText) findViewById(R.id.EditPassword);
   SharedPreferences settings = getSharedPreferences(SECTION_NAME, 0);
   settings.edit().putString(USERNAME, evUserName.getText().toString()).putString(PASSWORD,
     evPassword.getText().toString()).commit();

  }
 };
 
 private Button.OnClickListener saveToFile_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   EditText evUserName = (EditText) findViewById(R.id.EditUserName);
   EditText evPassword = (EditText) findViewById(R.id.EditPassword);
   String strUserName = evUserName.getText().toString();
   String strPassword = evPassword.getText().toString();

   DataOutputStream dos = null;
   try {
    dos = new DataOutputStream(openFileOutput(FILE_NAME, MODE_PRIVATE));
    dos.writeUTF(strUserName);
    dos.writeUTF(strPassword);
   } catch (IOException ioe) {
    ioe.printStackTrace();
   } finally {
    if (dos != null) {
     try {
      dos.close();
     } catch (IOException ioe) {
     }
    }
   }
  }
 };
在onCreate()重载函数的末尾添加如下的代码:
 load_values_from_file();
 attach_button_events();

在这部分中,需特别需要注意File的路径获取方法;

3. 上述代码贴完后,接下来将是测试部分,需要注意的是UserName与Password不能为空(为了简单,本程序未加对空字串的检测),
   产生完毕文件之后,可以在Window->Open Perspective->DDMS,即可打开DDMS视图,在此视图中,选择右边栏目的File Explorer之Tab view,打开如下地址:
data/data/com.example下面,分别查看shared_prefs与files目录下面,就会发现ExampleThree.xml与UserInfos.pwd分别对应本例中的SaveByPrefs以及Save by Self-defined file功能产生的文件。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jackxinxu2100/archive/2010/01/26/5258924.aspx

抱歉!评论已关闭.