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>
结果:
相关推荐
zhangyangffff 2011-07-12
Roka 2020-06-28
xhpscdx 2020-05-17
chaigang 2020-02-25
歆萌 2020-02-02
chunianyo 2019-12-21
疯狂紫萧 2019-12-14
PHP淮北PHP00网 2013-09-17
QCkiss 2019-11-04
歆萌 2019-11-03
Kwong 2019-10-22
宇智波鼬 2018-08-30
zhrookie 2011-11-18
81423067 2015-06-02
wewoor 2015-12-27
lizaochengwen 2019-03-19