android-file Storage-Text storage

Source: Internet
Author: User

[Return to Table of contents]

If you want to do a file input or output operation, you need to do the flow


Activity's support for file operations

Public FileInputStream openfileinput (String name) sets the file input stream to open

Public FileOutputStream openfileoutput (String name,int mode) sets the file output stream to be opened, specifying the mode of operation, which can be 0,mode_append, Mode_ PRIVATE, Mode_world_readable, mode_world_writeable

Public Resources getresources () Return Resources Object


Text storage is divided into the mobile phone space and sdcard, mobile phone space, the activity provided by the method to read and write, sdcard on the traditional IO operation.


Example of output saved in mobile space (file name does not need to write path, save directly under/data/data/package name/files)

<span Style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >package Com.example.testtext;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.printstream;import Android.app.activity;import Android.os.bundle;public class MainActivity extends Activity { Private final String filename= "potato.txt";//file name @overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); FileOutputStream fos=null;try {fos=super.openfileoutput (FILENAME, activity.mode_private); PrintStream ps=new printstream (FOS);p s.println ("potato");p s.println ("n");p s.println ("true");p s.close ();// Resources must be closed <span style= "Font-family:simhei;" > Fos.close (); </span><span style= "Font-family:simhei;" > </span> } catch (Exception e) {e.printstacktrace ();}}} </span> 

Files saved on the phone space are read
<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >package Com.example.testtext;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.util.scanner;import Android.app.activity;import Android.os.bundle;import Android.util.Log;public class Mainactivity extends Activity {private final String filename= "potato.txt"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); FileInputStream fis=null;try {fis=super.openfileinput (FILENAME); Scanner scanner=new Scanner (FIS), while (Scanner.hasnext ()) {LOG.E ("Potato", Scanner.next ());} Scanner.close (); Fis.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} </span>

Saved in the SDcard case

Review the IO stream operation steps in Java

1. Use the file class to define a document to manipulate

2. Instantiating a parent class using a byte stream or a subclass of a character stream (because four of the operation flows are abstract classes)

3. Completion of input or output functions

4. Close the stream

Example

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >package Com.example.testtext;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.printstream;import Java.util.Scanner ; Import Android.app.activity;import Android.os.bundle;import android.util.log;public class Mainactivity extends Activity {Private final String filename= "/mnt/sdcard/potato.txt"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); File File=new file (FILENAME), if (!file.getparentfile (). exists ()) {File.getparentfile (). Mkdirs ();} try {printstream ps=new printstream (new FileOutputStream (file));p s.println ("potato");p s.println ("tomato");p s.close ();} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} try {Scanner sn=new Scanner (new FileInputStream (file)), while (Sn.hasnext ()) {LOG.E ("Potato", Sn.next ());} Sn.close ();} catch (FILENOTFOundexception e) {e.printstacktrace ();}}} </span>


Note that the read-write SDcard needs to declare the permissions in the Androidmanifest.xml file, or it will error

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" ><uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE" ></uses-permission>< Uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission></span >








android-file Storage-Text storage

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.