Android access to file permissions four modes introduction _android

Source: Internet
Author: User
Tags file permissions

Access permissions for Linux files

* In Android, each application is an independent user
* DRWXRWXRWX
* 1th: D means folder,-indicates file
* 第2-4位: rwx, representing the owner of this file (the application that created the file) the user's permissions to the file
* R: Read
* W: Write
* x: Execute

* 第5-7位: RWX, which represents the permissions of users in the same group as the file owner user to the file

* 第8-10位: rwx, which means that users of other user groups have permissions to the file

Four modes of Openfileoutput

* MODE_PRIVATE:-RW-RW----

* MODE_APPEND:-RW-RW----

* mode_world_writeable:-rw-rw--w-

* mode_world_readable:-rw-rw-r--

The following combat:

Finish the 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/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=" button 1 "
android:onclick=" click1/>
</LinearLayout>

Add button Event

public void Click1 (View v) {
//data/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 on the button to form the corresponding file Info1.txt, as shown


And then generate the other button layouts:


The corresponding 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/data/com.wuyudong.permission.files try {fileoutputstream fos = openfileoutput ("inf
O1.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/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/data/com.wuyudong.permission.files try {FILEOUTPUTstream fos = openfileoutput ("Info3.txt", mode_world_readable);
Fos.write ("Global readable Mode". 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 ("Private Mode". GetBytes ());
Fos.close ();
catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();}} }

Click the button in turn to generate the appropriate permissions file:


Create an 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/data/com.wuyudong.permission/files/info3.txt"); try {fileinputst
Ream FIS = new FileInputStream (file);
Convert byte flows to character streams 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 ();}} }

The above is a small set to introduce the Android access to file permissions of four of the relevant content, I hope to help you, if you want to learn more content please pay attention to cloud Habitat community website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.