作者注:由于我在测试这个功能的时候发现文件名无法使用中文(sdk2.2 + 模拟器),如果有哪为高手无意中浏览此文章后,能对这个问题予以指点,我将感激不尽。呵呵。
********************注: 在Activity中有 getFileDir() 和 getCacheDir(); 方法可以获得当前的手机自带的存储空间中的当前包文件的路径
getFileDir() ----- /data/data/cn.xxx.xxx(当前包)/files
getCacheDir() ----- /data/data/cn.xxx.xxx(当前包)/cache
-
- 1. 编写文件读取与写入功能实现类 FileService
-
- package cn.android.service;
-
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
-
- import android.content.Context;
- import android.util.Log;
-
-
-
-
-
-
-
- public class FileService {
-
- public static final String TAG = "FileService";
- private Context context;
-
-
- public FileService(Context context) {
- this.context = context;
- }
-
-
-
-
-
-
-
-
- public void save(String fileName, String content) throws Exception {
-
-
- if (!fileName.endsWith(".txt")) {
- fileName = fileName + ".txt";
- }
-
- byte[] buf = fileName.getBytes("iso8859-1");
-
- Log.e(TAG, new String(buf,"utf-8"));
-
- fileName = new String(buf,"utf-8");
-
- Log.e(TAG, fileName);
-
-
-
-
-
-
-
-
- FileOutputStream fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
- fos.write(content.getBytes());
- fos.close();
- }
-
-
-
-
-
-
-
-
- public String read(String fileName) throws Exception {
-
-
- if (!fileName.endsWith(".txt")) {
- fileName = fileName + ".txt";
- }
-
- FileInputStream fis = context.openFileInput(fileName);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- byte[] buf = new byte[1024];
- int len = 0;
-
-
- while ((len = fis.read(buf)) != -1) {
- baos.write(buf, 0, len);
- }
-
- fis.close();
- baos.close();
-
-
- return baos.toString();
-
- }
-
- }
-
- 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 {
-
-
-
- private FileService fileService = new FileService(this);
-
- private EditText fileNameText;
-
- 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;
- }
-
-
-
-
- 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;
- }
-
-
-
-
- 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");
- }
- }
- });
-
-
- }
-
-
- private boolean isEmpty(String s) {
- if(s == null || "".equals(s.trim())) {
- return true;
- }
- return false;
- }
-
- }
-
- 3.文件布局文件:main.xml
- "1.0" encoding="utf-8"?>
- ""
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
-
-
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/filename"
- />
-
-
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/filename"
- />
-
-
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/content"
- />
-
-
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:minLines="3"
- android:id="@+id/content"
- />
-
-
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
-
-
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="@string/save"
- />
-
-
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/read"
- android:text="@string/read"
- />
-
-
-
-
阅读(1143) | 评论(0) | 转发(0) |