TXT file write:
1 Private voidSave () {2FileOutputStream fos =NULL;3String state = Environment.getexternalstoragestate ();//SD Status4 if(State.equals (environment.media_mounted)) {//determine if the SD card is available5File root =environment.getexternalstoragedirectory ();6File Targedir =NewFile (Root, Super.getpackagename ());//We're going to write the file accordingly under Activity .7 8 if(!targedir.exists ()) {//determine if a directory exists9Targedir.mkdir ();//CreateTen } One //String newfile=file.tostring () + "/" + "user.txt"; A //IO Operation - //Flow - Try { the //in parentheses is the path -FOS =NewFileOutputStream (NewFile (Targedir,"Userinfo.txt"));//output Stream -String content =name; - /* + * for (int i=0;i<content.length (); i++) { - * Fos.write (Content.charat (i));//byte output} + */ A byte[] data =content.getbytes (); at fos.write (data); - -}Catch(FileNotFoundException e) { - //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { in //IO exception is thrown by input and output - e.printstacktrace (); to}finally { + //Open the stream and you need to close it - Try { the fos.close (); *}Catch(IOException e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); - } the } + A } the}
TXT file read:
1 Private voidRead () {2FileInputStream FIS =NULL;3String State =environment.getexternalstoragestate ();4 if(State.equals (environment.media_mounted)) {5File root =environment.getexternalstoragedirectory ();6File File =NewFile (Root, super.getpackagename () +"/userinfo.txt");7 if(File.exists ()) {8 Try {9FIS =Newfileinputstream (file);Ten intLen =0; OneStringBuilder Builder =NewStringBuilder (); A byte[] buffer =New byte[1024x768]; - - while(len = fis.read (buffer))! =-1) { theBuilder.append (NewString (Buffer,0, Len)); - //builder.append (buffer);//garbled - } - /* + * while ((Len=fis.read ())!=-1) {builder.append ((char) len);} - */ + Toast.maketext (Getapplicationcontext (), builder.tostring (), A toast.length_short). Show (); at Text2.settext (builder.tostring ()); - -}Catch(FileNotFoundException e) { - //TODO auto-generated Catch block - e.printstacktrace (); -LOG.I ("Read", E.getmessage ()); in}Catch(IOException e) { - //TODO auto-generated Catch block to e.printstacktrace (); +}finally { - Try { the fis.close (); *}Catch(IOException e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); - } the } + } A } the}
copy of Picture:
Create an image folder under the Assets folder and put a picture in it.
1 Private voidcopy () {2FileOutputStream output =NULL;3InputStream Source =NULL;4String State =environment.getexternalstoragestate ();5 if(State.equals (environment.media_mounted)) {6 //inputstream=super.getresources (). Openrawresource (R.drawable.ic_launcher);7 8 Try {9Assetmanager Manager =super.getassets ();TenSource = Manager.open ("Image/ic_launcher.png"); OneFile target =NewFile ( AEnvironment.getexternalstoragedirectory () +"/" -+ super.getpackagename () +"/mycopyimg.png"); -Output =NewFileOutputStream (target); the - intLen =0; - byte[] buffer =New byte[1024x768]; - while(len = source.read (buffer))! =-1) { +Output.write (Buffer,0, Len); - } +}Catch(IOException e) { A //TODO auto-generated Catch block at e.printstacktrace (); -LOG.I ("111", E.getmessage ()); -}finally { - Try { - source.close (); - output.close (); in}Catch(IOException e) { - //TODO auto-generated Catch block to e.printstacktrace (); + } - } the } *}
Write, read, and copy pictures of the android. txt file