Android file access permission in four modes: android
Linux file access permission
* In Android, each application is an independent user.
* Drwxrwxrwx
* 1st-bit: d indicates a folder and-indicates a file.
* 2-4 bits: rwx, indicating the permission of the owner of the file (the application that creates the file) to the file
* R: Read
* W: Write
* X: Run
* 5-7: rwx, indicating the permissions of users in the same group as the file owner to the file
* 8-10 digits: rwx, indicating the permissions of users in other user groups on the file
Four modes of openFileOutput
* MODE_PRIVATE:-rw ----
* MODE_APPEND:-rw ----
* MODE_WORLD_WRITEABLE:-rw -- w-
* MODE_WORLD_READABLE:-rw-r --
The following practices:
Complete layout first
<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" Button 1 "android: onClick =" click1 "/> </LinearLayout>
Add button event
Public void click1 (View v) {// data/com. wuyudong. permission. files try {FileOutputStream fos = openFileOutput ("info1.txt", MODE_PRIVATE); fos. write ("Private mode ". getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}
Click the button to generate the corresponding file info1.txt,
Then generate other button la s:
The Code is as follows:
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/com. wuyudong. permission. files try {FileOutputStream fos = openFileOutput ("info1.txt", MODE_PRIVATE); fos. write ("Private mode ". getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public void click2 (View v) {// data/com. wuyudong. permission. files try {FileOutputStream fos = openFileOutput ("info2.txt", MODE_APPEND); fos. write ("APPEND mode ". getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public void click3 (View v) {// data/com. wuyudong. permission. files try {FileOutputStream fos = openFileOutput ("info3.txt", MODE_WORLD_READABLE); fos. write ("global read mode ". getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public void click4 (View v) {// data/com. wuyudong. permission. files try {FileOutputStream fos = openFileOutput ("info4.txt", MODE_WORLD_WRITEABLE); fos. write ("Private mode ". getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
Click the button in sequence to generate the file with the corresponding permissions:
Create another application to read the previously generated info3.txt file.
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/com. wuyudong. permission/files/info3.txt "); try {FileInputStream FCM = new FileInputStream (file); // convert byte stream to bytes stream BufferedReader br = new BufferedReader (new InputStreamReader (FS )); string text = br. readLine (); Toast. makeText (this, text, 0 ). show ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}