这篇文章主要介绍了 Android 实现文件的保存与读取功能, 涉及 Android 中文件操作函数 getFileDir() 和 getCacheDir() 的相关使用技巧, 需要的朋友可以参考下
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
本文实例讲述了 Android 实现文件的保存与读取功能。分享给大家供大家参考,具体如下:
注: 在 Activity 中有 getFileDir() 和 getCacheDir(); 方法可以获得当前的手机自带的存储空间中的当前包文件的路径
getFileDir() ----- /data/data/cn.xxx.xxx(当前包)/files
getCacheDir() ----- /data/data/cn.xxx.xxx(当前包)/cache
1. 编写文件读取与写入功能实现类 FileService
2. 编写 Activity 类:
- package cn.android.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import cn.android.service.FileService;
- public class TestAndroidActivity extends Activity {
- /** Called when the activity is first created. */
- //得到FileService对象
- private FileService fileService = new FileService(this);
- //定义视图中的filename输入框对象
- private EditText fileNameText;
- //定义视图中的contentText输入框对象
- private EditText contentText;
- //定义一个土司提示对象
- private Toast toast;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //得到视图中的两个输入框和两个按钮的对象引用
- Button button = (Button)this.findViewById(R.id.button);
- Button read = (Button)this.findViewById(R.id.read);
- fileNameText = (EditText) this.findViewById(R.id.filename);
- contentText = (EditText) this.findViewById(R.id.content);
- //为保存按钮添加保存事件
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String fileName = fileNameText.getText().toString();
- String content = contentText.getText().toString();
- //当文件名为空的时候,提示用户文件名为空,并记录日志。
- if(isEmpty(fileName)) {
- toast = Toast.makeText(TestAndroidActivity.this, R.string.empty_filename, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.w(fileService.TAG, "The file name is empty");
- return;
- }
- //当文件内容为空的时候,提示用户文件内容为空,并记录日志。
- if(isEmpty(content)) {
- toast = Toast.makeText(TestAndroidActivity.this, R.string.empty_content, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.w(fileService.TAG, "The file content is empty");
- return;
- }
- //当文件名和内容都不为空的时候,调用fileService的save方法
- //当成功执行的时候,提示用户保存成功,并记录日志
- //当出现异常的时候,提示用户保存失败,并记录日志
- try {
- fileService.save(fileName, content);
- toast = Toast.makeText(TestAndroidActivity.this, R.string.success, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.i(fileService.TAG, "The file save successful");
- } catch (Exception e) {
- toast = Toast.makeText(TestAndroidActivity.this, R.string.fail, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.e(fileService.TAG, "The file save failed");
- }
- }
- });
- //为读取按钮添加读取事件
- read.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //得到文件名输入框中的值
- String fileName = fileNameText.getText().toString();
- //如果文件名为空,则提示用户输入文件名,并记录日志
- if(isEmpty(fileName)) {
- toast = Toast.makeText(TestAndroidActivity.this, R.string.empty_filename, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.w(fileService.TAG, "The file name is empty");
- return;
- }
- //调用fileService的read方法,并将读取出来的内容放入到文本内容输入框里面
- //如果成功执行,提示用户读取成功,并记录日志。
- //如果出现异常信息(例:文件不存在),提示用户读取失败,并记录日志。
- try {
- contentText.setText(fileService.read(fileName));
- toast = Toast.makeText(TestAndroidActivity.this, R.string.read_success, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.i(fileService.TAG, "The file read successful");
- } catch (Exception e) {
- toast = Toast.makeText(TestAndroidActivity.this, R.string.read_fail, Toast.LENGTH_LONG);
- toast.setMargin(RESULT_CANCELED, 0.345f);
- toast.show();
- Log.e(fileService.TAG, "The file read failed");
- }
- }
- });
- }
- //编写一个isEmpty方法,判断字符串是否为空
- private boolean isEmpty(String s) {
- if(s == null || "".equals(s.trim())) {
- return true;
- }
- return false;
- }
- }
3. 文件布局文件:main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/filename"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/filename"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/content"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:minLines="3"
- android:id="@+id/content"
- />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="@string/save"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/read"
- android:text="@string/read"
- />
- </LinearLayout>
- </LinearLayout>
PS:由于我在测试这个功能的时候发现文件名无法使用中文(sdk2.2 + 模拟器), 如果有哪为高手无意中浏览此文章后,能对这个问题予以指点,我将感激不尽。呵呵。
希望本文所述对大家 Android 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0323/247086.html