Android memory file read/write: No Permissions required
Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton FileSave; PrivateButton FileRead; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); FileSave=(Button) Findviewbyid (R.id.button1); FileRead=(Button) Findviewbyid (R.id.button2); Filesave.setonclicklistener ( This); Fileread.setonclicklistener ( This); } @Override Public voidOnClick (View v) {intID =V.getid (); Switch(ID) { CaseR.id.button1:fileoutputstream Fos=NULL; Try{fos= Openfileoutput ("OUT.txt", context.mode_private); Fos.write ("Text ...". GetBytes ()); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(Fos! =NULL) { Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } } Break; CaseR.id.button2:fileinputstream fis=NULL; Try{FIS= Openfileinput ("OUT.txt"); byte[] B =New byte[1024]; intLen = 0; StringBuilder SB=NewStringBuilder (); while(len = Fis.read (b))! =-1) {string string=NewString (b, 0, Len); Sb.append (string); } toast.maketext ( This, Sb.tostring (), Toast.length_short). Show (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(FIS! =NULL) { Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } } } Break; default: Break; } }}
Android memory file read/write