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

本文介绍了一个Android应用如何通过SharedPreferences和自定义文件存储两种方式进行数据保存的具体实现。文章详细展示了存储和读取用户名与密码的过程,并提供了完整的代码示例。


在此实例中,重点展示使用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功能产生的文件。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值