Data storage and Access under Android (1)---in the form of a file

Source: Internet
Author: User

Data storage and Access under Android (1)---in the form of a file 1.1 storage files stored in the phone memory:/* * * * * * * * * * * * * * * * * * * * * * */data/data/Package name/files/jxn.txt file string data = "Test";///data/ data/Package name/filesfile Filesdir = Context.getfilesdir (); File File = new file (Filesdir, "jxn.txt"); FileOutputStream fos = new FileOutputStream (file), Fos.write (Data.getbytes ()); Fos.flush (); Fos.close ();/* * * from/data/ data/Packet name/files/jxn.txt file read Data string "test"; File Filesdir = Context.getfilesdir (); File File = new file (Filesdir, "jxn.txt"); FileInputStream fis = new FileInputStream (file); BufferedReader reader = new BufferedReader (new InputStreamReader (FIS)); String text = Reader.readline (); Reader.close (); add: 1, the Openfileoutput () method in the Android context can write data to the/data/data/package name/files Directory 2, In the Android context, the Openfileinput () method can read file 3 under the/data/data/package name/files directory, and the implementation process is the same as saving the data in the J2SE environment to the file. The first parameter of the eg://Openfileoutput () method is used to specify the file name, cannot contain the path delimiter "/", and if the file does not exist, Android will automatically create//store the path:/data/data/Package name/files/ Jxn.txtfileoutputstream OutStream = context.openfileoutput ("Jxn.txt", context.mode_private); The sharedpreferences1,android providesA sharedpreferences class, which is a lightweight storage class that is especially suitable for saving software configuration parameters. 2, the use of sharedpreferences to save data, and finally the data is stored in an XML file, the file is stored in the/data/data/package name/shared_prefs directory 3,context.getsharedpreferences (name , mode) method is used to specify the name of the file without a suffix, and Android automatically adds an. xml suffix. Store data into/data/data/package name/shared_prefs/jxn.xml File///data/data/package name/shared_prefs/jxn.xmlsharedpreferences SP = Context.getsharedpreferences ("Jxn", context.mode_private);//Get an Editor Object Editor edit = Sp.edit ();// Save Data edit.putstring ("number", number), edit.putstring ("password", password);//Submit, data is saved to file Edit.commit ();/* * * FROM/ Read data in data/data/package name/shared_prefs/jxn.xml file Sharedpreferences sp = context.getsharedpreferences ("Jxn", Context.MODE_ PRIVATE); String number = sp.getstring ("number", null); String Password = sp.getstring ("password", NULL), 1.2 stored files stored in SD card:/* * * * * * * * * * * * * * * * * * * * * * * To determine whether the current phone has an SD card If there is an SD card and can read and write, the method returns environment.media_mountedstring state = Environment.getexternalstoragestate (); Environment.MEDIA_MOUNTED.equals (state)) {return false;} String data = "Test";/mnt/sdcard but different phone's SD card directory may be different file Sdcardfile = Environment.getexternalstoragedirectory (); File File = new file (sdcardfile, "jxn.txt"); FileOutputStream fos = new FileOutputStream (file), Fos.write (Data.getbytes ()); Fos.flush (); Fos.close ();/* * * from/mnt/ Read data in the Sdcard/jxn.txt file//Determine if the current phone has an SD card string state = Environment.getexternalstoragestate (); Environment.MEDIA_MOUNTED.equals (state)) {return null;} File sdcardfile = Environment.getexternalstoragedirectory (); File File = new file (sdcardfile, "jxn.txt"); BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file)); String text = Br.readline (); Br.close ();<!--setting permissions to write and read SD cards--><uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/><uses-permission android:name=" android.permission.READ_ External_storage "/>

Data storage and Access under Android (1)---in the form of a file

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.