Android--file Storage Operations (i)

Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Write the built-in space output write</p></p><p><p>2. Write SDcard card input read</p></p><p><p>The file pointer can be read by the Getfilepointer method and set through the Seek Method.</p></p><p><p><span style="color: #ff0000;">Randomaccessfile also integrates FileInputStream and FileOutputStream together,</span> and supports reading or writing data from any byte of a file, and the Randomaccessfile class provides a mechanism for Equivalent to inserting a pointer in the file stream that can be read on demand.</p></p><p><p>File is only an abstraction of a document or directory, can be imagined as a file handle, identity, the class itself only provides open, close, delete, attribute access to the file, and so on;</p></p><p><p>File storage is simply a general javase in the IO stream, just to apply him to the Android phone only;</p></p><p><p><strong>1 File storage</strong></p></p><p><p><strong>The core is the Openfileinput () and Openfileoutput () methods of the Context. then you can read and write with the Java Stream.</strong></p></p><pre> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">to save a piece of text to a file:</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">The Save () method is called in Ondestory (). This method is written under Super.ondestory ().<br>Openfileoutput</span> --fileoutputstream--outputstreamreader-bufferedreader</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Save () {String data</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Data to save</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; FileOutputStream</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; BufferedWriter writer</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span>= Mcontext.openfileoutput (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Data</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, context.mode_private);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">a <span style="color: #008000;">filenot error is required.</span></span>writer =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>BufferedWriter (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>OutputStreamWriter (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> out</span>));<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Build a outputstreamwriter. then a bufferedwriter () is Constructed.</span></span>Writer.write (data);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">IO error Required</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(filenotfoundexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(writer! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{writer.close (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } } } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read data from a file the next time it is opened<br>Openfileinput--fileinputstream--inputstreamreader-bufferedreader<br></span></span>The first two file is always in front of input<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String load () {fileinputstream</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; BufferedReader Reader</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; StringBuilder SB</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span>= Mcontext.openfileinput (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Data</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Filenot Error Required</span></span>Reader =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>BufferedReader (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>InputStreamReader (<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">)); String</span> line</span>=<span style="color: #800000;"><span style="color: #800000;">""</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(line = Reader.readline ())! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">IO error Required</span></span><span style="color: #000000;"><span style="color: #000000;">Sb.append (line); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(filenotfoundexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(reader! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{reader.close (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">sb.tostring (); }</span></span></pre><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">write to built-in storage space</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Write (view view) {String inputstring</span></span>=<span style="color: #000000;"><span style="color: #000000;">inputtext.gettext (). toString (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(inputstring = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>||<span style="color: #800000;"><span style="color: #800000;">""</span></span><span style="color: #000000;"><span style="color: #000000;">. Equals (inputstring.trim ())) {toast.maketext (mcontext,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Please enter what you want to write, not empty</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } FileOutputStream Fos</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(appendbtn.ischecked ()) {fos</span></span>=<span style="color: #000000;"><span style="color: #000000;">mcontext.openfileoutput (fileName, context.mode_append); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{fos</span></span>=<span style="color: #000000;"><span style="color: #000000;">mcontext.openfileoutput (fileName, context.mode_private); } fos.write (inputstring.getbytes ()); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">write content to a file</span></span>Toast.maketext (mcontext,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">the <span style="color: #800000;">content is written! </span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(filenotfoundexception e) {toast.maketext (mcontext,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">File not found, Please check file path</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception e) {toast.maketext (mcontext,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">error IO occurred during write</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read the built-in storage space</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Readfromsdcard (view View) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(environment.getexternalstoragestate (). equals (ENVIRONMENT.MEDIA_MOUNTED)) {String path</span></span>= Environment.getexternalstoragedirectory () + file.separator +<span style="color: #000000;"><span style="color: #000000;">fileName; File File</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (path); StringBuilder Sbuilder</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>StringBuilder (<span style="color: #800000;"><span style="color: #800000;">""</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[] buffer =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[<span style="color: #800080;"><span style="color: #800080;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Hasread =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{randomaccessfile RAF</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Randomaccessfile (file,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">RW</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>((hasread = Raf.read (buffer))! =-<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">) {sbuilder.append (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>String (buffer,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">, hasread)); } Outputtext.settext (sbuilder); Raf.close (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(filenotfoundexception e) {toast.maketext (mcontext,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">File not found, Please check file path</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception e) {toast.maketext (mcontext,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">error IO occurred during write</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); } } }</span></span></pre><p><p></p></p><p><p></p></p><p><p>The default is saved in The/data/data/<packagename>/files/directory.</p></p><p><p>The second is <span style="color: #ff0000;">the operation model of the file</span> , there are two main modes to choose</p></p> <ol> <ol> <li><span style="color: #ff0000;">mode_private</span> (overwrite, is the default mode of Operation)</li> <li><span style="color: #ff0000;">Mode_append</span> (append) does not exist create</li> <li>Mode_world_readable allows other applications to read and write to the Program. but it's too dangerous to be abandoned after 4.2.</li> <li>Mode_world_writeable</li> </ol> </ol><p><p></p></p><p><p><span style="color: #ff0000;">Edit.setselection (inputtext.length ());</span></p></p><p><p><span style="color: #ff0000;">Textutils.isempty () method, which is useful when you can judge both states (null and equal to an empty String) and return True</span></p></p><p><p></p></p><p><p>Android--file Storage Operations (i)</p></p></span>

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.