欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-04-24 16:57:58
MainActivity.java activity_main.xml 结果: ......

MainActivity.java

 1 package com.example.prim;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 
 7 import android.os.Bundle;
 8 import android.app.Activity;
 9 import android.view.Menu;
10 import android.view.View;
11 
12 public class MainActivity extends Activity {
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18     }
19 
20 //点击按钮生成private文件
21 public void click1(View v){
22      try {
23         FileOutputStream fos=openFileOutput("private.txt",MODE_PRIVATE);
24         fos.write("private".getBytes());
25         fos.close();
26     } catch (Exception e) {
27         // TODO Auto-generated catch block
28         e.printStackTrace();
29     }
30 }
31     
32 //点击按钮生成append文件
33 public void click2(View v){
34      try {
35         FileOutputStream fos=openFileOutput("append.txt",MODE_APPEND);
36         fos.write("append".getBytes());
37         fos.close();
38     } catch (Exception e) {
39         // TODO Auto-generated catch block
40         e.printStackTrace();
41     }
42 }
43 //点击按钮生成read文件
44 public void click3(View v){
45      try {
46         FileOutputStream fos=openFileOutput("read.txt",MODE_WORLD_READABLE);
47         fos.write("read".getBytes());
48         fos.close();
49     } catch (Exception e) {
50         // TODO Auto-generated catch block
51         e.printStackTrace();
52     }
53 }
54 //点击按钮生成write文件
55 public void click4(View v){
56      try {
57         FileOutputStream fos=openFileOutput("write.txt",MODE_WORLD_WRITEABLE);
58         fos.write("write".getBytes());
59         fos.close();
60     } catch (Exception e) {
61         // TODO Auto-generated catch block
62         e.printStackTrace();
63     }
64 }
65 
66 
67 }

activity_main.xml

 1 <LinearLayout 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     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <Button
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:onClick="click1"
12         android:text="private" />
13 
14     <Button
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:onClick="click2"
18         android:text="append" />
19 
20     <Button
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:onClick="click3"
24         android:text="read" />
25 
26     <Button
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:onClick="click4"
30         android:text="write" />
31 
32 </LinearLayout>

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