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)