Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中 TXT 文件的讀寫封裝

編輯:關於 Android 程式設計

Android 中 TXT 文件的讀寫

Android 的文件讀寫分幾種常見場景:應用私有目錄(/data/data/包名/files)、SD 卡(/mnt/sdcard)、raw 資源目錄以及 assets 目錄。下面用一個工具類把這些操作統一封裝起來。

package com.wirelessqa.helper;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.util.EncodingUtils;
import android.app.Activity;

public class FileAccess extends Activity {

    // 一、私有目錄下的讀寫(/data/data/包名/files)
    public void writeFileData(String fileName, String message) {
        try {
            FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
            fout.write(message.getBytes());
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String readFileData(String fileName) {
        String res = "";
        try {
            FileInputStream fin = openFileInput(fileName);
            byte[] buffer = new byte[fin.available()];
            fin.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    // 二、SD 卡目錄下的讀寫(/mnt/sdcard/)
    // 注意:SD 卡要用 FileOutputStream/FileInputStream,不能用 openFileOutput
    public void writeFileSdcard(String fileName, String message) {
        try {
            FileOutputStream fout = new FileOutputStream(fileName);
            fout.write(message.getBytes());
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String readFileSdcard(String fileName) {
        String res = "";
        try {
            FileInputStream fin = new FileInputStream(fileName);
            byte[] buffer = new byte[fin.available()];
            fin.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    // 三、從 raw 資源讀取(只讀)
    public String readFromRaw(int fileInRaw) {
        String res = "";
        try {
            InputStream in = getResources().openRawResource(fileInRaw);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            res = EncodingUtils.getString(buffer, "GBK");
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    // 四、從 assets 讀取(只讀)
    public String readFromAsset(String fileName) {
        String res = "";
        try {
            InputStream in = getResources().getAssets().open(fileName);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}

幾個要點:私有目錄用 openFileOutput/openFileInput 最方便;SD 卡則必須用文件流,並提前申請 WRITE_EXTERNAL_STORAGE 權限;raw 與 assets 裡的資源只能讀不能寫,讀取時還要注意編碼(示例中 raw 用 GBK、其餘用 UTF-8,需按實際文件編碼調整)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved