"Android" 14.1 Internal file storage and reading

Source: Internet
Author: User
<span id="Label3"></p><p><p>Category: C #, Android, VS2015;</p></p><p><p>Date created: 2016-02-27 I. INTRODUCTION</p></p><p><p>Internal storage refers to the private files that are created by the application are stored in internal storage (the smaller mobile phone card sold by the mobility reseller).</p></p><p><p>Applications can read these files on the internal storage device through the Openfileinput method and the Openfileoutput Method.</p></p><p><p>1. Openfileoutput method</p></p><p><p>This method opens the Application's private file and prepares it for writing to the Device. By default, files that are written overwrite the original file with the same name. If the file you want to open does not exist, a new file is Created.</p></p><p><p>The syntax format for this method is as Follows:</p></p><p><p>public override Stream Openfileoutput (string name, filecreationmode Mode)</p></p><p><p>The mode parameter allows you to change how the file is Written:</p></p> <ul> <ul> <li>Filecreationmode.private: Private Mode (default). Files can only be accessed by programs created, or by programs with the same UID.</li> <li>Filecreationmode.append: Append mode, The file does not exist in the first create and then add, exists just at the end of the original file to add new Data.</li> <li>Filecreationmode.worldreadable: Global Read mode, which allows any program to read private Files.</li> <li>Filecreationmode.worldwriteable: Global Write mode, which allows any program to write to private files.</li> </ul> </ul> <p><p>For example:</p></p><p><p>String Filename= "a1.txt";</p></p><p><p>var f=openfileoutput (fileName, filecreationmode.private);</p></p><p><p>String text= "some data";</p></p><p><p>byte[] bytes = System.Text.Encoding.UTF8.GetBytes (str);</p></p><p><p>F.write (bytes, 0, Bytes. Length);</p></p><p><p>F.flush ();</p></p><p><p>F.close ();</p></p><p><p>Note: for performance reasons, Android will put the written data into the data buffer before accumulating to some extent to write to the file, so before calling the close method, be sure to call the Flush method to ensure that the buffer data is written to the File.</p></p><p><p>In addition, in order to avoid forgetting to close the stream caused a memory overflow, it is most like the complete example below this section to enclose it with a using Statement.</p></p><p><p>2. Openfileinput method</p></p><p><p>This method opens the Application's private file and prepares it for Reading. The syntax format for this method is as Follows:</p></p><p><p>public override Stream Openfileinput (string Name)</p></p><p><p>Note: name refers to a file name without a path.</p></p><p><p>Whether you read a file or write a file, it is possible to create an exception, and in practice, always do not forget to use Try/catch to catch these Exceptions. As an example, these exceptions are no longer captured in order to avoid diluting the content of Interest.</p></p><p><p>3. Static methods provided by the System.IO.File class</p></p><p><p>In addition to the above two methods, can also be used Directly. NET provides a static method of the System.IO.File class to read and write to internal storage files, and is easier to use. second, Example 1 run</p></p><p><p></p></p>third, the main design steps<p><p>1. Add ch1401_main.axml File</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"><?</span></span><span style="color: #ff00ff"><span style="color: #ff00ff">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff"><span style="color: #0000ff">?></span></span><span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">LinearLayout</span></span><span style="color: #ff0000"><span style="color: #ff0000">xmlns:android</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:orientation</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "vertical"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Button</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:id</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@+id/btnwrite"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "wrap_content"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:text</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "write Private file to internal memory"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Button</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:id</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@+id/btnread"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "wrap_content"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:text</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "read private files in internal memory"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">TextView</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:text</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "Small Text"</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:textappearance</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "? android:attr/textappearancesmall"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_width</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "match_parent"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:id</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@+id/textview1"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_margintop</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "80dp"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:background</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@android: color/holo_green_dark"</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:gravity</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "fill_vertical"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:shadowradius</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "+"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:textcolor</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@android: color/white"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_marginleft</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "40dp"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_marginright</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "40dp"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_marginbottom</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "40dp"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:layout_height</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "match_parent"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span><span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">LinearLayout</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span></pre><p><p>2. Add Ch1401MainActivity.cs file</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">system.io;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">system.text;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.app;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.content;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.os;</span></span><span style="color: #0000ff"><span style="color: #0000ff">using</span></span><span style="color: #000000"><span style="color: #000000">android.widget;</span></span><span style="color: #0000ff"><span style="color: #0000ff">namespace</span></span><span style="color: #000000"><span style="color: #000000">mydemos.srcdemos{[Activity (Label</span></span>=<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">"example 14-1" Internal file access</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> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">ch1401mainactivity:activity {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Private</span></span><span style="color: #000000"><span style="color: #000000">Stream stream; </span></span><span style="color: #0000ff"><span style="color: #0000ff">Private</span></span> <span style="color: #0000ff"><span style="color: #0000ff">string</span></span>FileName =<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">A1.txt</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">protected</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Override</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">OnCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Base</span></span><span style="color: #000000"><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (Resource.Layout.ch1401_Main); Stream</span></span>=<span style="color: #000000"><span style="color: #000000">openfileoutput (fileName, filecreationmode.private); </span></span><span style="color: #0000ff"><span style="color: #0000ff">string</span></span>FilePath =<span style="color: #000000"><span style="color: #000000">Getfilestreampath (fileName). Path; </span></span><span style="color: #0000ff"><span style="color: #0000ff">var</span></span>TextView1 = findviewbyid<textview><span style="color: #000000"><span style="color: #000000">(Resource.Id.textView1); Button Btnwrite</span></span>= findviewbyid<button><span style="color: #000000"><span style="color: #000000">(Resource.Id.btnWrite); Btnwrite.click</span></span>+=<span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">usage not using android-provided APIs</span></span> <span style="color: #0000ff"><span style="color: #0000ff">string</span></span>str =<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000">hello <span style="color: #800000">, Hi. </span></span><span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #000000"><span style="color: #000000">; File.writealltext (filePath, str, encoding.utf8); Textview1.text</span></span>= $<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000">the <span style="color: #800000">file has been written to the Memory Card. \ n Write content: {str}\n file location: \n{filepath}</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: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">usage 2--using android-provided APIs</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">using (var f = openfileoutput (fileName, Filecreationmode.private))</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">{ </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">string str = "hello, hi." "; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">byte[] bytes = System.Text.Encoding.UTF8.GetBytes (str); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">f.write (bytes, 0, bytes. Length); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">F.flush (); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">FilePath = Getfilestreampath (fileName). Path; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">textview1.text = $ "the file has been written to the Memory Card. \ n Write content: {str}\n file location: \n{filepath} "; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">}</span></span><span style="color: #000000"><span style="color: #000000"> }; Button Btnread</span></span>= findviewbyid<button><span style="color: #000000"><span style="color: #000000">(Resource.Id.btnRead); Btnread.click</span></span>+=<span style="color: #0000ff"><span style="color: #0000ff">Delegate</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Usage 1</span></span> <span style="color: #0000ff"><span style="color: #0000ff">string</span></span>s =<span style="color: #000000"><span style="color: #000000">File.readalltext (filePath, encoding.utf8); Textview1.text</span></span>= $<span style="color: #800000"><span style="color: #800000">"</span></span><span style="color: #800000"><span style="color: #800000">Read File: \n{filepath}\n read content: \n{s}</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: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Usage 2</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">using (var f = openfileinput (fileName))</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">{ </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">string s = ""; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">byte[] bytes = new byte[1024]; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">int n; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000">while <span style="color: #008000">((n = f.read (bytes, 0, 1024x768)) > 0)</span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000"> { </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">s + = System.Text.Encoding.UTF8.GetString (bytes, 0, n); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000"> } </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Textview1.text = $ "read file: \n{filepath}\n Read Content: \n{s}"; </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">}</span></span><span style="color: #000000"><span style="color: #000000"> }; } }}</span></span></pre><p><p>"Android" 14.1 Internal file storage and reading</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.