android点击生成private文件,append文件,read文件,write文件权限案例

MainActivity.java

package com.example.prim;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 
 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.view.View;
 
 public class MainActivity extends Activity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
 
 //点击按钮生成private文件
 public void click1(View v){
      try {
         FileOutputStream fos=openFileOutput("private.txt",MODE_PRIVATE);
         fos.write("private".getBytes());
         fos.close();
     } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }
     
 //点击按钮生成append文件
 public void click2(View v){
      try {
         FileOutputStream fos=openFileOutput("append.txt",MODE_APPEND);
         fos.write("append".getBytes());
         fos.close();
     } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }
 //点击按钮生成read文件
 public void click3(View v){
      try {
         FileOutputStream fos=openFileOutput("read.txt",MODE_WORLD_READABLE);
         fos.write("read".getBytes());
         fos.close();
     } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }
 //点击按钮生成write文件
 public void click4(View v){
      try {
         FileOutputStream fos=openFileOutput("write.txt",MODE_WORLD_WRITEABLE);
         fos.write("write".getBytes());
         fos.close();
     } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }
 
 
 }

activity_main.xml

<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"
     tools:context=".MainActivity" >
 
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="click1"
         android:text="private" />
 
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="click2"
         android:text="append" />
 
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="click3"
         android:text="read" />
 
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="click4"
         android:text="write" />
 
 </LinearLayout>

结果:android点击生成private文件,append文件,read文件,write文件权限案例

相关推荐