File permission actions in Android

Source: Internet
Author: User
Tags readfile

The default file created by this project has read and write access to the project.

We can pass Context.openfileoutput ("filename", mode);

We can create private, shared, read-only, write-only files, default text private files.

If other Android projects access this project's files will be limited, the Android kernel is Linux, so his file management and Linux files in the same time.

To create a file code:

/** * Create various files * @throws IOException **/@SuppressLint ({"Worldwriteablefiles","Worldreadablefiles"}) @SuppressWarnings ("Resource")    Private voidCreateFile () throws IOException {log.i (TAG,"start creating a file");                FileOutputStream Fos; //Create a private fileFOS = This. Openfileoutput ("Private.txt", context.mode_private); Fos.write ("Private". GetBytes ());                Fos.close (); //Create a public fileFOS = This. Openfileoutput ("Public.txt", Context.mode_world_readable +context.mode_world_writeable); Fos.write (" Public". GetBytes ());                Fos.close (); //Create default FileFile File =NewFile ( This. Getfilesdir (),"Default.txt"); FOS=Newfileoutputstream (file); Fos.write ("default". GetBytes ());                Fos.close (); //Create a read-only fileFOS = This. Openfileoutput ("Readable.txt", context.mode_world_readable); Fos.write ("readable". GetBytes ());                Fos.close (); //Create a write-only fileFOS = This. Openfileoutput ("Writeable.txt", context.mode_world_writeable); Fos.write ("writeable". GetBytes ());                Fos.close (); Toast.maketext ( This,"file creation succeeded", Toast.length_long). Show (); LOG.I (TAG,"Create file Complete"); }
View Code

Othera Android Project access to files created by the Filespower project

Package Com.example.other;import java.io.bufferedreader;import java.io.bufferedwriter;import java.io.File; Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.filewriter;import Java.io.ioexception;import Android.annotation.SuppressLint; Import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.Toast; Public classMainactivity extends actionbaractivity implements onclicklistener{PrivateString TAG ="mainactivity"; /** Read and write private files*/    PrivateButton btnwriteprivate, btnreadprivate; /** Read and write common documents*/    PrivateButton btnwritepublic, btnreadpublic; /** Read and write default files*/    PrivateButton Btnwritedefalut, Btnreaddefault; /** Read-only file reading and writing*/    PrivateButton btnwritereadable, btnreadreadable; /** Read and write only files*/    PrivateButton btnwritewriteable, btnreadwriteable; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //Component AcquisitionBtnwriteprivate =(Button) Findviewbyid (r.id.btn_writeprivate); Btnreadprivate=(Button) Findviewbyid (r.id.btn_readprivate); Btnwritepublic=(Button) Findviewbyid (r.id.btn_writepublic); Btnreadpublic=(Button) Findviewbyid (r.id.btn_readpublic); Btnwritedefalut=(Button) Findviewbyid (R.id.btn_writedefault); Btnreaddefault=(Button) Findviewbyid (R.id.btn_readdefault); Btnwritereadable=(Button) Findviewbyid (r.id.btn_writereadable); Btnreadreadable=(Button) Findviewbyid (r.id.btn_readreadable); Btnwritewriteable=(Button) Findviewbyid (r.id.btn_writewriteable); Btnreadwriteable=(Button) Findviewbyid (r.id.btn_readwriteable); //Setting up Listener eventsBtnwriteprivate.setonclicklistener ( This); Btnreadprivate.setonclicklistener ( This); Btnwritepublic.setonclicklistener ( This); Btnreadpublic.setonclicklistener ( This); Btnwritedefalut.setonclicklistener ( This); Btnreaddefault.setonclicklistener ( This); Btnwritereadable.setonclicklistener ( This); Btnreadreadable.setonclicklistener ( This); Btnwritewriteable.setonclicklistener ( This); Btnreadwriteable.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn_readprivate:readfile ("Private.txt");  Break;  CaseR.id.btn_readpublic:readfile ("Public.txt");  Break;  CaseR.id.btn_readdefault:readfile ("Default.txt");  Break;  CaseR.id.btn_readreadable:readfile ("Readable.txt");  Break;  CaseR.id.btn_readwriteable:readfile ("Writeable.txt");  Break;  CaseR.id.btn_writeprivate:writefile ("Private.txt");  Break;  CaseR.id.btn_writepublic:writefile ("Public.txt");  Break;  CaseR.id.btn_writedefault:writefile ("Default.txt");  Break;  CaseR.id.btn_writereadable:writefile ("Readable.txt");  Break;  CaseR.id.btn_writewriteable:writefile ("Writeable.txt");  Break; default:             Break; }} @SuppressLint ("Sdcardpath")     Public voidreadFile (String fileName) {log.i (TAG,"Read the file"+fileName); File File=NewFile ("/data/data/com.example.filepower/files/"+fileName); Try{@SuppressWarnings ("Resource") BufferedReader BR=NewBufferedReader (Newfilereader (file)); String Context=Br.readline (); Toast.maketext ( This, Context, Toast.length_short). Show (); } Catch(Exception e) {Toast.maketext ( This,"Read failed", Toast.length_short). Show (); }} @SuppressLint ("Sdcardpath") @SuppressWarnings ("Resource")     Public voidWriteFile (String fileName) {log.i (TAG,"Write a file"+fileName); File File=NewFile ("/data/data/com.example.filepower/files/", FileName); Try{FileOutputStream fos=Newfileoutputstream (file); Fos.write ("XXX". GetBytes ()); } Catch(Exception e) {Toast.maketext ( This,"failed to write file", Toast.length_short). Show (); }    }}
View Code

Document Explanation:

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.