Implementation of the read and write of the android file

Source: Internet
Author: User

Description

File save path is android default path,/data/app package name (full name)/files

First, activity

Package Com.example.filerw;import Com.example.server.fileserver;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.widget.EditText;import Android.widget.textview;public class Mainactivity extends Activity {private EditText contentedit;private String file_ Name= "file.txt";p rivate fileserver fileserver;private TextView showtext;protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ContentEdit = ( EditText) Findviewbyid (r.id.content) Showtext = (TextView) Findviewbyid (r.id.showtext); fileserver = new Fileserver ( Getapplicationcontext ());} public void Mainclick (View v) {switch (V.getid ()) {case R.id.save:string str = Contentedit.gettext (). toString (); Fileserver.save (file_name, str); Break;case r.id.read:string show = Fileserver.read (file_name); Showtext.settext (show ); break;default:break;}} Menu public boolean oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate(R.menu.main, menu); return true;}} 
Second, the main realizationCode

Package Com.example.server;import Java.io.bytearrayoutputstream;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Org.apache.http.util.encodingutils;import Android.content.context;public class Fileserver {private Context context;private FileInputStream in;private FileOutputStream Out;public Fileserver (context context) {This.context = context;} /** * Save * @param name */public void Save (String name,string content) {try {out = Context.openfileoutput (name, Context.mode _private); Out.write (Content.getbytes ()); Out.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (Exception e) {System.out.println (e.tostring ());}} /** * Read File * @param name * @return */public string Read (string name) {String file_content = null;try {Bytearrayoutputstream Byteout = new Bytearrayoutputstream (); in = Context.openfileinput (name);//Open file with buffer instance byte[] buf = new Byte[1024];while (( In.read (BUF))!=-1) {byteout.write (BUF);//Writes the read-out data to the cache}file_content = EncOdingutils.getstring (Byteout.tobytearray (), "GBK");//Transcode Byteout.close (); In.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (Exception e) {System.out.println (e.tostring ());} return file_content;}}
Third, XML

<relativelayout 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:paddi ngbottom= "@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 "android:padding=" 0DP "> <edittext android:id=" @+id/content "android:inputtype=" text        "Android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:hint=" Please enter the content "    Android:textsize= "16sp" android:background= "@drawable/edit_xmlbg" android:minheight= "30DP"/> <textview android:id= "@+id/showtext" android:layout_width= "fill_parent" android:layout_height= "WR Ap_content "android:layout_below=" @+id/contenT "/> <button android:id=" @+id/save "android:onclick=" Mainclick "Android:layout_widt        H= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "18SP" android:text= "Save" Android:layout_alignparentbottom= "true"/><button android:id= "@+id/read" android:onclick= " Mainclick "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:textSize= "18SP" android:text= "read" android:layout_alignparentbottom= "true" android:layout_torightof= "@+id/sav E "/></relativelayout>




//

Implementation of the read and write of the android file

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.