Three kinds are used:
Mode_private/mode_append: The two are each start to re-write, one is the append mode. But the access rights are the same for both.
Mode_world_readable: Global readable mode, can be read by other applications, not recommended
Mode_world_writeable: Global writable mode, can be read by other applications, not recommended
The code tests are as follows:
Packagecom.swust.access;ImportJava.io.FileOutputStream;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } //button One Click event Public voidonclick (View v) {//path already defaults to data/data/package name/files//private and Append permissions are the same Try{FileOutputStream fos= Openfileoutput ("Info.txt", mode_private); Fos.write ("I'm the King of Bubbles.". GetBytes ()); Fos.close (); }Catch(Exception e) {e.printstacktrace (); } } //button 2 Click event Public voidOnclick1 (View v) {//Global readable mode, not recommended Try{FileOutputStream fos= Openfileoutput ("Info1.txt", mode_world_readable); Fos.write ("Global readable mode, not recommended". GetBytes ()); Fos.close (); }Catch(Exception e) {e.printstacktrace (); } } //Button 3 Click event Public voidOnclick2 (View v) {//Global writable mode, not recommended Try{FileOutputStream fos= Openfileoutput ("Info2.txt", mode_world_writeable); Fos.write ("Global writable mode, not recommended". GetBytes ()); Fos.close (); }Catch(Exception e) {e.printstacktrace (); } }}
Effect:
When the third button of the app is written to read-write (warning: Not used, here for testing)
The code is as follows, using the OR statement connection:
button 3 Click event public void Onclick2 (View v) {
// global writable readable mode, deprecated try {fileoutputstream fos = openfileoutput ("Info2.txt" , mode_world_writeable | mode_world_readable); Fos.write ( Global writable and readable, where the next app is tested to read the files of the Access app. The use of ".getbytes ()) is not recommended; Fos.close (); catch (Exception e) {E.PR Intstacktrace (); } }
At this time
Build another Android app to test:
Code, by the way, by using the file method to read files, used by the project:
//Hazard testing: To read readable and writable files from other applications//By the way, read by using file files (for projects) Public voidonclick (View v) {//access to other application file paths must be handwritten, because the API can only access the application'sFile File =NewFile ("Data/data/com.swust.access/files/info2.txt"); Try{FileInputStream fis=Newfileinputstream (file); BufferedReader BR=NewBufferedReader (NewInputStreamReader (FIS)); Toast.maketext ( This, Br.readline (), 1). Show (); }Catch(Exception e) {e.printstacktrace (); } }
Effect:
As you can see, files that can be accessed to other apps are generally not recommended!
Android file access rights mode configuration