当前位置:操作系统 > 安卓/Android >>

Android的SD卡文件读写

 

\

使用右上角的两个按钮可以将文件从模拟器中导出和导入

\

程序运行的结果

\

运行之后,文件浏览器中的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) { 

        &nbs

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,