Android的SD卡文件讀寫 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

使用右上角的兩個按鈕可以將文件從模擬器中導出和導入

程序運行的結果

運行之後,文件瀏覽器中的delete被刪除瞭。

 

 

FileHelper.java是文件的幫助類,完成文件創建、刪除、讀。

 

 

package com.zeph.android.fileoperate; 

 

import java.io.File; 

import java.io.FileInputStream; 

import java.io.FileNotFoundException; 

import java.io.IOException; 

 

import android.content.Context; 

import android.os.Environment; 

 

public class FileHelper { 

 

    private Context context; 

    /** SD卡是否存在**/ 

    private boolean hasSD = false; 

    /** SD卡的路徑**/ 

    private String SDPATH; 

    /** 當前程序包的路徑**/ 

    private String FILESPATH; 

 

    public FileHelper(Context context) { 

        this.context = context; 

        hasSD = Environment.getExternalStorageState().equals( 

                android.os.Environment.MEDIA_MOUNTED); 

        SDPATH = Environment.getExternalStorageDirectory().getPath(); 

        FILESPATH = this.context.getFilesDir().getPath(); 

    } 

 

    /**

     * 在SD卡上創建文件

     * 

     * @throws IOException

     */ 

    public File createSDFile(String fileName) throws IOException { 

        File file = new File(SDPATH + "//" + fileName); 

        if (!file.exists()) { 

            file.createNewFile(); 

        } 

        return file; 

    } 

 

    /**

     * 刪除SD卡上的文件

     * 

     * @param fileName

     */ 

    public boolean deleteSDFile(String fileName) { 

        File file = new File(SDPATH + "//" + fileName); 

        if (file == null || !file.exists() || file.isDirectory()) 

            return false; 

        return file.delete(); 

    } 

 

    /**

     * 讀取SD卡中文本文件

     * 

     * @param fileName

     * @return

     */ 

    public String readSDFile(String fileName) { 

        StringBuffer sb = new StringBuffer(); 

        File file = new File(SDPATH + "//" + fileName); 

        try { 

            FileInputStream fis = new FileInputStream(file); 

            int c; 

            while ((c = fis.read()) != -1) { 

                sb.append((char) c); 

            } 

            fis.close(); 

        } catch (FileNotFoundException e) { 

            e.printStackTrace(); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } 

        return sb.toString(); 

    } 

 

    public String getFILESPATH() { 

        return FILESPATH; 

    } 

 

    public String getSDPATH() { 

        return SDPATH; 

    } 

 

    public boolean hasSD() { 

        return hasSD; 

    } 

 

Activity類

 

 

 

package com.zeph.android.fileoperate; 

 

import java.io.IOException; 

 

import android.app.Activity; 

import android.os.Bundle; 

import android.widget.TextView; 

 

public class FileOperateActivity extends Activity { 

 

    private TextView hasSDTextView; 

    private TextView SDPathTextView; 

    private TextView FILESpathTextView; 

    private TextView createFileTextView; 

    private TextView readFileTextView; 

    private TextView deleteFileTextView; 

    private FileHelper helper; 

 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

 

        hasSDTextView = (TextView) findViewById(R.id.hasSDTextView); 

        SDPathTextView = (TextView) findViewById(R.id.SDPathTextView); 

        FILESpathTextView = (TextView) findViewById(R.id.FILESpathTextView); 

        createFileTextView = (TextView) findViewById(R.id.createFileTextView); 

        readFileTextView = (TextView) findViewById(R.id.readFileTextView); 

        deleteFileTextView = (TextView) findViewById(R.id.deleteFileTextView); 

 

        helper = new FileHelper(getApplicationContext()); 

 

        hasSDTextView.setText("SD卡是否存在:" + helper.hasSD()); 

        SDPathTextView.setText("SD卡路徑:" + helper.getSDPATH()); 

        FILESpathTextView.setText("包路徑:" + helper.getFILESPATH()); 

 

        try { 

            createFileTextView.setText("創建文件:" 

                    + helper.createSDFile("test.txt").getAbsolutePath()); 

        } catch (IOException e) { 

            e.printStackTrace(); 

        } 

 

        deleteFileTextView.setText("刪除文件是否成功:" 

                + helper.deleteSDFile("delete.txt")); 

 

        readFileTextView.setText("讀取文件:" + helper.readSDFile("benzeph.txt")); 

    } 

 

Layout.xml

 

 

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

 

    <TextView 

        android:id="@+id/hasSDTextView" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="@string/hello" /> 

 

    <TextView 

        android:id="@+id/SDPathTextView" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="@string/hello" /> 

 

    <TextView 

        android:id="@+id/FILESpathTextView" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="@string/hello" /> 

 

    <TextView 

        android:id="@+id/createFileTextView" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="false" /> 

     

    <TextView 

        android:id="@+id/readFileTextView" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="false" /> 

     

    <TextView 

        android:id="@+id/deleteFileTextView" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="false" /> 

 

</LinearLayout> 

 

註意一定要在Manifest中加入讀取外部設備的條件允許,我之前就是一直忘記加入,導致文件老是不能創建和刪除。

 

 

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

 

摘自 Ben Zeph's Code Cloud

發佈留言