This year looked at the Android file storage, roughly the same as the Java implementation
Android provides the following two ways to open the IO stream of data files for this application:
FileInputStream Openfileinput (fileName); Input stream
FileOutputStream outputstream = Openfileoutput (FileName, mode_private); Output stream
The second way to open the output stream is to indicate the mode of the open file, which is about a few:
Mode_private: The file can only be read and written by the current program and is overwritten
Mode_append: Open the file as an append to add content to the file
Mode_world_readable: The contents of this file can be read by other applications
Mode_world_writeable: The contents of this file can be read and written by other programs
The following simple implementation reads the input text from a edittext and displays it in the TextView
Mainactivity.java
Package com.file.filetest;import java.io.bufferedreader;import java.io.bufferedwriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; import java.io.ioexception;import java.io.inputstreamreader;import java.io.outputstreamwriter; import java.io.unsupportedencodingexception;import com.file.filetest.r;import android.app.activity;import android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import android.widget.button;import android.widget.edittext;import android.widget.textview;public class mainactivity extends activity {public static String FILE_NAME = "Testfile.bin";p rivate edittext edittext;private textview textview;private button readbutton;private button writebutton;@ Overrideprotected void oncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);readButton = ( button) findviewbyid (r.id.button2);writebutton = (Button) findviewbyid (R.id.button1); edittext = (EditText) findviewbyid (R.ID.EDITTEXT1);textview = (TextView) Findviewbyid (R.id.textview1); Readbutton.setonclicklistener (New onclicklistener () {@Overridepublic void onclick (view v) {textview.settext (ReadFile (file_name));}); Writebutton.setonclicklistener (New onclicklistener () {@Overridepublic void onclick (View  V) {writefile (File_name, edittext.gettext (). toString ());}}); Private string readfile (String filename) {bufferedreader breader = null;try {fileinputstream instream = openfileinput (fileName); inputstreamreader reader = new inputstreamreader (instream, "GBK"); Breader = new buffeRedreader (reader); Stringbuffer stringbuffer = new stringbuffer (""); string str;while ((Str = breader.readline ()) != null) {stringBuffer.append ( STR);} Return stringbuffer.tostring ();} catch (filenotfoundexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (ioexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} finally {if (Breader != null) {try {breader.close ();} catch (ioexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();}}} return "";} Private void writefile (String filename, string context) {BufferedWriter writer = null; Fileoutputstream outputstream;try {outputstream = openfileoutput (fileName, MODE_ PRIVATE); outputstreamwriter outputwriter = new outputstreamwriter (outputstream, "GBK"); Writer = new bufferedwriter ( Outputwriter); Writer.write (context); Writer.newline (); Writer.flush ();} catch (filenotfoundexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (unsupportedencodingexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} catch (ioexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} finally {if (Writer != null) {try {writer.close ();} catch (ioexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();}}}}
Layout file Main.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" tools:context= "${ Relativepackage}.${activityclass} " > <EditText android:id= "@+id/edittext1" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android: Ems= "Ten" > <requestFocus /> </edittext> &Nbsp; <textview android:id= "@+id/textView1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android: Layout_alignparentleft= "true" android:layout_below= "@+id/ EditText1 " android:text=" TextView " /> <button android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_ Alignparentleft= "true" android:layout_below= "@+id/textView1" android:layout_margintop= "98DP" android:text= "write " /> <button android:id=" @+id /button2 " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignbaseline= "@+id/button1" android: Layout_alignbottom= "@+id/button1" android:layout_ Centerhorizontal= "true" android:text= "read" /></ Relativelayout>
The layout is as follows:
The above basic implementation of the file read and write function, but this can only meet the input only text and number of cases, when the input image (such as input a smiley face), there is a problem, in the internet roughly searched, and did not find a better solution, text mixed file, there are 4 problems to solve:
-
Identify input bitmap from EditText and record
-
Save text and bitmap to file, bitmap to encode separately
-
Parsing text messages and bitmap (decoding) from a file
-
Text and pictures in TextView display from a file-solution using Spannablestring class
Looking for answers from the great God
File files in Android store and read bitmap in file