Android中的多種檔案讀寫操作方法

來源:互聯網
上載者:User

 

package com.ppmeet; 

 

import java.io.File; 

import java.io.FileInputStream; 

import java.io.FileOutputStream; 

import java.io.IOException; 

import java.io.InputStream; 

 

import org.apache.http.util.EncodingUtils; 

 

import android.content.Context; 

 

/**

 * class name:FileService<BR>

 * class description:android檔案的一些讀取操作<BR>

 * PS:<BR>

 * 

 * @version 1.00 2010/10/21

 * @author CODYY)peijiangping

 */ 

public class FileService { 

    private Context context; 

 

    public FileService(Context c) { 

        this.context = c; 

    } 

 

    // 讀取sd中的檔案 

    public String readSDCardFile(String path) throws IOException { 

        File file = new File(path); 

        FileInputStream fis = new FileInputStream(file); 

        String result = streamRead(fis); 

        return result; 

    } 

 

    // 在res目錄下建立一個raw資源檔夾,這裡的檔案只能讀不能寫入。。。 

    public String readRawFile(int fileId) throws IOException { 

        // 取得輸入資料流 

        InputStream is = context.getResources().openRawResource(fileId); 

        String result = streamRead(is);// 返回一個字串 

        return result; 

    } 

 

    private String streamRead(InputStream is) throws IOException { 

        int buffersize = is.available();// 取得輸入資料流的位元組長度 

        byte buffer[] = new byte[buffersize]; 

        is.read(buffer);// 將資料讀入數組 

        is.close();// 讀取完畢後要關閉流。 

        String result = EncodingUtils.getString(buffer, "UTF-8");// 設定取得的資料編碼,防止亂碼 

        return result; 

    } 

 

    // 在assets檔案夾下的檔案,同樣是只能讀取不能寫入 

    public String readAssetsFile(String filename) throws IOException { 

        // 取得輸入資料流 

        InputStream is = context.getResources().getAssets().open(filename); 

        String result = streamRead(is);// 返回一個字串  

        return result; 

    } 

 

    // 往sd卡中寫入檔案 

    public void writeSDCardFile(String path, byte[] buffer) throws IOException { 

        File file = new File(path); 

        FileOutputStream fos = new FileOutputStream(file); 

        fos.write(buffer);// 寫入buffer數組。如果想寫入一些簡單的字元,可以將String.getBytes()再寫入檔案; 

        fos.close(); 

    } 

 

    // 將檔案寫入應用的data/data的files目錄下 

    public void writeDateFile(String fileName, byte[] buffer) throws Exception { 

        byte[] buf = fileName.getBytes("iso8859-1"); 

        fileName = new String(buf, "utf-8"); 

        // Context.MODE_PRIVATE:為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案的內容,如果想把新寫入的內容追加到原檔案中。可以使用Context.MODE_APPEND 

        // Context.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。 

        // Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有許可權讀寫該檔案。 

        // MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取;MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入。 

        // 如果希望檔案被其他應用讀和寫,可以傳入: 

        // openFileOutput("output.txt", Context.MODE_WORLD_READABLE + 

        // Context.MODE_WORLD_WRITEABLE); 

        FileOutputStream fos = context.openFileOutput(fileName, 

                Context.MODE_APPEND);// 添加在檔案後面 

        fos.write(buffer); 

        fos.close(); 

    } 

 

    // 讀取應用的data/data的files目錄下檔案資料 

    public String readDateFile(String fileName) throws Exception { 

        FileInputStream fis = context.openFileInput(fileName); 

        String result = streamRead(fis);// 返回一個字串 

        return result; 

    } 

 

這裡提供一個我寫好的封裝類。

 

解釋都放注釋裡面了。

摘自 裴裴的Android學習和分享

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.