How to store files in Android memory J

Source: Internet
Author: User

Supports reading and writing data to a file, creating a new file

Main

 Packagecom.chuanxidemo.shaoxin.demo06;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;/*** Created by Shaoxin on 2017/2/23.*/ Public classMainextendsappcompatactivity {PrivateButton Read; PrivateButton Write; PrivateFileOutputStream OutputStream; PrivateFileInputStream InputStream; @Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Read=(Button) Findviewbyid (R.id.read); Write=(Button) Findviewbyid (r.id.write); Read.setonclicklistener (NewMyclick ()); Write.setonclicklistener (NewMyclick ()); }     Public classMyclickImplementsView.onclicklistener {@Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.write:Try{OutputStream= Openfileoutput ("Test.txt", Mode_append); Outputstream.write ("123". GetBytes ()); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {                        Try {                            if(OutputStream! =NULL) {outputstream.close (); }                        } Catch(IOException e) {e.printstacktrace (); }                    }                     Break;  CaseR.id.read:Try{InputStream= Openfileinput ("Test.txt"); byte[] bytes =New byte[1024]; intnum = 0; intI =0;  while(num = Inputstream.read ())!=-1) {Bytes[i]= (byte) num; I++; } log.i ("MSG", "OnClick:" +NewString (bytes,0, i)); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {                        if(inputstream!=NULL){                            Try{inputstream.close (); } Catch(IOException e) {e.printstacktrace (); }                        }                    }                     Break; }        }    }}

Main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <ButtonAndroid:id= "@+id/read"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Read File" />    <ButtonAndroid:id= "@+id/write"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Write file" /></LinearLayout>

How to store files in Android memory J

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.