Linux文件的訪問許可權* 在Android中,每一個應用是一個獨立的用戶* drwxrwxrwx* 第1位:d表示文件夾,-表示文件* 第2-4位:rwx,表示這個文件的擁有者(創建這個文件的應用)用戶對該文件的許可權 * r:讀 * w:寫 * x:執行* 第5-7位:rwx,表示跟文件擁有者用戶 ...
Linux文件的訪問許可權
* 在Android中,每一個應用是一個獨立的用戶
* drwxrwxrwx
* 第1位:d表示文件夾,-表示文件
* 第2-4位:rwx,表示這個文件的擁有者(創建這個文件的應用)用戶對該文件的許可權
* r:讀
* w:寫
* x:執行
* 第5-7位:rwx,表示跟文件擁有者用戶同組的用戶對該文件的許可權
* 第8-10位:rwx,表示其他用戶組的用戶對該文件的許可權
openFileOutput的四種模式
* MODE_PRIVATE:-rw-rw----
* MODE_APPEND:-rw-rw----
* MODE_WORLD_WRITEABLE:-rw-rw--w-
* MODE_WORLD_READABLE:-rw-rw-r--
下麵實戰一下:
首先完成佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" android:onClick="click1" /> </LinearLayout>
添加按鈕事件
public void click1(View v) { //data/data/com.wuyudong.permission.files try { FileOutputStream fos = openFileOutput("info1.txt", MODE_PRIVATE); fos.write("私有模式".getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
點擊按鈕後生成相應的文件info1.txt,如圖
然後再生成其他的按鈕佈局:
相應的代碼如下:
package com.wuyudong.permission; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.os.Bundle; import android.app.Activity; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click1(View v) { // data/data/com.wuyudong.permission.files try { FileOutputStream fos = openFileOutput("info1.txt", MODE_PRIVATE); fos.write("私有模式".getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void click2(View v) { // data/data/com.wuyudong.permission.files try { FileOutputStream fos = openFileOutput("info2.txt", MODE_APPEND); fos.write("追加模式".getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void click3(View v) { // data/data/com.wuyudong.permission.files try { FileOutputStream fos = openFileOutput("info3.txt", MODE_WORLD_READABLE); fos.write("全局可讀模式".getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void click4(View v) { // data/data/com.wuyudong.permission.files try { FileOutputStream fos = openFileOutput("info4.txt", MODE_WORLD_WRITEABLE); fos.write("私有模式".getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
依次點擊按鈕,生成相應許可權的文件:
再創建一個應用來讀取之前生成的info3.txt文件
package com.wuyudong.other; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v) { File file = new File("data/data/com.wuyudong.permission/files/info3.txt"); try { FileInputStream fis = new FileInputStream(file); //把位元組流轉換成字元流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String text = br.readLine(); Toast.makeText(this, text, 0).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }