1 Public classMainactivityextendsactionbaractivity {2 3 FinalString file_name = "Crazyit.bin";4 @Override5 protected voidonCreate (Bundle savedinstancestate) {6 Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8 9 TenSystem.out.println (NewStringBuilder ("a"). Append ("B"). Append ("C").). toString ()); One AButton Read =(Button) Findviewbyid (r.id.read); -Button Write =(Button) Findviewbyid (r.id.write); - the - FinalEditText edit1 =(EditText) Findviewbyid (r.id.edit1); - FinalEditText Edit2 =(EditText) Findviewbyid (r.id.edit2); - +Write.setonclicklistener (NewOnclicklistener () { - + Public voidOnClick (View v) { A Write (Edit1.gettext (). toString ()); atEdit1.settext (""); - } - }); - -Read.setonclicklistener (NewOnclicklistener () { - in Public voidOnClick (View v) { - Edit2.settext (read ()); to } + }); - the } * $ Public voidWrite (String content) {Panax Notoginseng Try { -FileOutputStream fos =openfileoutput (file_name, mode_append); thePrintStream PS =NewPrintStream (FOS); + ps.println (content); A ps.close (); the +}Catch(FileNotFoundException e) { - e.printstacktrace (); $ } $ - } - the PublicString Read () { - Try {WuyiFileInputStream FIS =Openfileinput (file_name); the byte[] Buff =New byte[1024]; - intHasread = 0; WuStringBuilder Builder =NewStringBuilder (""); - while((Hasread = Fis.read (buff)) > 0 ) { AboutBuilder.append (NewString (Buff, 0, Hasread)); $ } - fis.close (); - returnbuilder.tostring (); - A}Catch(FileNotFoundException e) { + e.printstacktrace (); the}Catch(IOException e) { - e.printstacktrace (); $ } the the return NULL; the the
The XML layout file is as follows: <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <button android:id= "@+id/read" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Read"/> <button android:id= "@+id/write" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" Write "/> <edittext Android:id= "@+id/edit1" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:ems= "Ten" > <requestfocus/> </EditText> <edittext android:id = "@+id/edit2" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android: Ems= "Ten"/></linearlayout>
+}
Android file read Simple operation