Android學習——文件存儲

 

在Andriod開發中,文件存儲和Java的文件存儲相似。但須要注意的是,爲了防止產生碎片垃圾,在建立文件時,要儘可能使用系統給出的函數進行建立,這樣當APP被卸載後,系統能夠將這些文件統一刪除掉。獲取文件的方式主要有如下幾種。數組

File file1 =this.getFilesDir();//獲取當前程序默認的數據存儲目錄
        Log.d("Jinx",file1.toString());
        File file2 =this.getCacheDir(); //默認的緩存文件存儲位置
        Log.d("Jinx",file2.toString());
        File file3=this.getDir("test",MODE_PRIVATE);    //在存儲目錄下建立該文件
        Log.d("Jinx",file3.toString());
        File file4=this.getExternalFilesDir(Environment.DIRECTORY_MUSIC);   //獲取外部存儲文件
        Log.d("Jinx",file4.toString());
        File file5=this.getExternalCacheDir();  //獲取外部緩存文件
        Log.d("Jinx",file5.toString());

 

相應的Log日誌以下,根據日誌,能夠很清楚看到每種方法獲取到的文件的區別:緩存

03-28 03:19:06.948 12690-12690/com.example.jinx.file D/Jinx: /data/user/0/com.example.jinx.file/files
03-28 03:19:06.948 12690-12690/com.example.jinx.file D/Jinx: /data/user/0/com.example.jinx.file/cache
03-28 03:19:06.948 12690-12690/com.example.jinx.file D/Jinx: /data/user/0/com.example.jinx.file/app_test
03-28 03:19:06.963 12690-12690/com.example.jinx.file D/Jinx: /storage/emulated/0/Android/data/com.example.jinx.file/files/Music
03-28 03:19:06.966 12690-12690/com.example.jinx.file D/Jinx: /storage/emulated/0/Android/data/com.example.jinx.file/cache

 

固然,若是有一些重要的文件,不想在APP被刪除時丟失,則能夠自定義文件路徑,以下所示:app

File file=new File("/mmt/sdcard/test");
        if(!file.exists())
        {
            Toast.makeText(MainActivity.this,"good!",Toast.LENGTH_SHORT).show();
            try
            {
                file.createNewFile();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            Toast.makeText(MainActivity.this,"good!",Toast.LENGTH_SHORT).show();
        }

 

讀寫文件操做


Android經過自帶的FileInputStream和FileOutputStream來進行文件的讀寫,具體代碼以下:函數

1.文件寫入:

private void WriteText(String text)
    {
        try
        {
            FileOutputStream fos=openFileOutput("a.txt",MODE_APPEND);   //獲取FileOutputStream對象
            fos.write(text.getBytes()); //寫入字節
            fos.close();    //關閉文件流
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

 

2.文件讀取

private String Read()
    {
        String text=null;
        try
        {
            FileInputStream fis=openFileInput("a.txt"); //獲取FileInputStream對象
            //ByteArrayOutputStream來存放獲取到的數據信息
            ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
            
            byte [] buffer=new byte[1024];  //建立byte數組,分屢次獲取數據
            int len=0;
            while ((len=fis.read(buffer))!=-1)  //經過FileInputStream的read方法來讀取信息
            {
                baos.write(buffer,0,len);   //ByteArrayOutputStream的write方法來寫入讀到的數據
            }
            text=baos.toString();
            fis.close();
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return text;
    }
相關文章
相關標籤/搜索