Android IO stream operation file (store and read)

Source: Internet
Author: User

To store files:

 Public classFileoperate extends Activity {Private StaticFinal String FILENAME ="MyData.txt";//Set file name    Private StaticFinal String DIR ="Ljpdata" ; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Super.setcontentview (R.layout.main); //Call Layout file        if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//if SDcard existsFile File =NewFile (Environment. getExternalStorageDirectory (). toString ()+File.separator+ DIR + file.separator + FILENAME);//defining the File class object            if(! File.getparentfile (). exists ()) {//parent folder does not existFile.getparentfile (). Mkdirs ();//Create a folder} printstream out=NULL;//print Stream object for output            Try {                 out=NewPrintStream (NewFileOutputStream (file,true));//Append file                 out. println ("School of Software, Shandong University (sdu,www.sdu.edu.cn), Lecturer: Student"); } Catch(Exception e) {e.printstacktrace (); } finally {                if( out!=NULL) {                     out. Close ();//Close the print stream                }            }        } Else{//SDcard does not exist, prompting the user with ToastToast.maketext ( This,"save failed, SD card does not exist! ", Toast.length_long). Show (); }    }}

Read file:

 Public classFileoperate extends Activity {Private StaticFinal String FILENAME ="MyData.txt";//Set file name    Private StaticFinal String DIR ="Ljpdata";//Set Save Folder    PrivateTextView msg =NULL;//Text Display@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Super.setcontentview (R.layout.main); //Call Layout file         This. msg =(TextView) Super.findviewbyid (r.id.msg); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//if SDcard existsFile File =NewFile (Environment. getExternalStorageDirectory (). toString ()+File.separator+ DIR + file.separator + FILENAME);//defining the File class object            if(! File.getparentfile (). exists ()) {//parent folder does not existFile.getparentfile (). Mkdirs ();//Create a folder} Scanner Scan=NULL;//Scan Input            Try{Scan=NewScanner (NewFileInputStream (file));//Instantiate scanner                 while(Scan.hasnext ()) {//Loop Read                     This. Msg.append (Scan.next () +"") ; Set Text                }            } Catch(Exception e) {e.printstacktrace (); } finally {                if(Scan! =NULL) {scan.close (); //Close the print stream                }            }        } Else{//SDcard does not exist, prompting the user with ToastToast.maketext ( This,"read failed, SD card does not exist! ", Toast.length_long). Show (); }    }}

Android transforms the image into stream storage and converts the flow to an image

 // take the image to byte[]  byte  [] byteicon = Usericon;  //  if  (Byteicon! = null   = new   Bytearrayinputstream (Byteicon); Usericondrawable  = Drawable.createfromstream (Bais,  " image   );}  
BitMap Usericon; // convert bitmap to drawable New Bytearrayoutputstream (); // parameter 1 conversion type, parameter 2 compression quality, parameter 3 byte throttle resource          os); // turn the drawable into bitmap Bitmapdrawable tempdrawable = (bitmapdrawable) Drawableimage;tempdrawable.getbitmap ();

Android IO stream operation file (store and read)

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.