Android(java)學習筆記184:產生4種模式的檔案

來源:互聯網
上載者:User

標籤:

1.首先我們編寫一個產生4種模式的檔案的程式案例:

(1)首先是activity_main.xml檔案:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     tools:context=".MainActivity" > 6  7     <Button 8         android:onClick="click" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:layout_centerHorizontal="true"12         android:layout_centerVertical="true"13         android:text="組建檔案" />14 15     <RadioGroup16         android:id="@+id/rg_mode"17         android:layout_width="wrap_content"18         android:layout_height="wrap_content" >19 20         <RadioButton21             android:id="@+id/rb_private"22             android:layout_width="wrap_content"23             android:layout_height="wrap_content"24             android:checked="true"25             android:text="私人 private" />26 27         <RadioButton28             android:id="@+id/rb_readble"29             android:layout_width="wrap_content"30             android:layout_height="wrap_content"31             android:text="全域可讀 world readable" />32 33         <RadioButton34             android:id="@+id/rb_writeable"35             android:layout_width="wrap_content"36             android:layout_height="wrap_content"37             android:text="全域可寫world writeable" />38 39         <RadioButton40             android:id="@+id/rb_public"41             android:layout_width="wrap_content"42             android:layout_height="wrap_content"43             android:text="全域可讀可寫public" />44     </RadioGroup>45 46 </RelativeLayout>

布局效果如:

 

(2)接下來是代碼邏輯部分如下:MainActivity.java:

 1 package com.itheima.filemode; 2  3 import java.io.FileOutputStream; 4  5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.RadioGroup; 9 10 public class MainActivity extends Activity {11     private RadioGroup rg_mode;12 13     @Override14     protected void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16         setContentView(R.layout.activity_main);17         rg_mode = (RadioGroup) findViewById(R.id.rg_mode);18     }19 20     /**21      * 按鈕的點擊事件22      * 23      * @param view24      */25     public void click(View view) {26         try {27             int id = rg_mode.getCheckedRadioButtonId();28             FileOutputStream fos = null;29             switch (id) {30             case R.id.rb_private:// 私人檔案31                 fos = openFileOutput("private.txt",  MODE_PRIVATE);32                 break;33             case R.id.rb_public:// 可讀可寫檔案34                 fos = openFileOutput("public.txt",  MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);35                 break;36             case R.id.rb_readble:// 全域可讀檔案37                 fos = openFileOutput("readble.txt",  MODE_WORLD_READABLE);38                 break;39             case R.id.rb_writeable:// 全域可寫檔案40                 fos = openFileOutput("/sdcard/files/writeable.txt",  MODE_WORLD_WRITEABLE);41                 break;42             }43             fos.write("dfafda".getBytes());44             fos.close();45         } catch (Exception e) {46             e.printStackTrace();47         }48     }49 }

openFileOutput()方法的第一參數用於指定檔案名稱,不能包含路徑分隔字元“/” ,如果檔案不存在,Android 會自動建立它。建立的檔案儲存在/data/data/<package name>/files目錄,如: /data/data/cn.itcast.action/files/itcast.txt ,通過點擊Eclipse菜單“Window”-“Show View”-“Other”,在交談視窗中展開android檔案夾,選擇下面的File Explorer視圖,然後在File Explorer視圖中展開/data/data/<package name>/files目錄就可以看到該檔案。

我們分別點擊介面上不同的RadioButton,產生不同檔案如:

2.小結:android下檔案訪問的許可權:

* 預設情況下所有的檔案建立出來都是私人的。只有自己的應用程式可以訪問裡面的資料,別的應用程式是不可以訪問資料的。
* 特殊情況利用api可以修改檔案的許可權。
    openFileOutput("檔案名稱","檔案的訪問模式"); 私人 唯讀 唯寫 可讀可寫
* 底層是通過Linux作業系統的檔案模式來實現的。

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

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

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

MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取;

MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入。

Android(java)學習筆記184:產生4種模式的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.