【安卓學(xué)習(xí)筆記】文件存儲(chǔ)讀寫

安卓的文件存儲(chǔ)讀寫主要有兩種:

1、data/data/包名/files 目錄下存儲(chǔ)。

這種存儲(chǔ)是將文件存到手機(jī)內(nèi)存中對應(yīng)的包名下。軟件卸載后,存在這里的文件也全部被系統(tǒng)刪除。此外,存在這里的文件可定義訪問權(quán)限:

圖片截取自菜鳥教程
2、SD卡下存儲(chǔ)。

常用于一些媒體文件,如圖片、音視頻、大文件等。

使用示例:

1、讀寫包名下文件

public class FileHelp {

        private Context context;

        public FileHelp(Context context) {
            this.context = context;
        }

        public void fileWrite(String fileName, String fileContent) {

            try {
                FileOutputStream fos = context.openFileOutput(fileName,
                        MODE_PRIVATE);
                fos.write(fileContent.getBytes());
                fos.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public String fileRead(String fileName) {

            try {
                FileInputStream fis = context.openFileInput(fileName);
                StringBuffer sb = new StringBuffer();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) != -1) {
                    sb.append(new String(buffer, 0, len));
                }
                fis.close();
                return sb.toString();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

    }

2、SD卡讀寫

public class SDFileHelper {

        public void fileWrite(String fileName, String content) {

            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                try {
                    fileName = Environment.getExternalStorageDirectory()
                            .getCanonicalPath() + "/" + fileName;

                    FileOutputStream fos = new FileOutputStream(fileName);

                    fos.write(content.getBytes());
                    fos.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public String fileRead(String fileName) {

            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                fileName = Environment.getExternalStorageDirectory() + "/"
                        + fileName;

                try {
                    FileInputStream fis = new FileInputStream(fileName);
                    StringBuffer sb = new StringBuffer();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = fis.read(buffer)) != -1) {
                        sb.append(new String(buffer, 0, len));
                    }
                    fis.close();
                    return sb.toString();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            return null;
        }

    }

這兩種方式基本差不多,區(qū)別在于方式1是openFileInput/openFileOutput,方式2是直接new一個(gè)文件流。另外,方式2需要先判斷SD卡是否掛載,然后獲取SD卡根目錄。

參考自 http://www.runoob.com/w3cnote/android-tutorial-file.html
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容