Data storage and access under Android---in the form of files

Source: Internet
Author: User

Data storage and access under Android---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 folder 2. In the Android context, the Openfileinput () method can read file 3 under the/data/data/Package name/files folder, and the detailed implementation process is the same as saving data in a file in a J2SE environment.

The first parameter of the eg://Openfileoutput () method is used to specify the file name and cannot include the path delimiter "/", assuming the file does not exist. Android will voluntarily create//store path:/data/data/package name/files/jxn.txtfileoutputstream OutStream = Context.openfileoutput ("Jxn.txt", Context.mode_private); Sharedpreferences1,android provides a sharedpreferences class. It is a lightweight storage class. Ideal for saving software configuration parameters. 2. Use Sharedpreferences to save the data, which is the end of the XML file to hold the data, the file is stored in the/data/data/package name/shared_prefs folder 3,context.getsharedpreferences (name, mode) The first parameter of the method is used to specify the name of the file without a suffix, and Android will actively add the. 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, the data is saved to the file Edit.commit ();/* * * FROM/ data/data/Packet name/shared_prefs/jxn.xml file read Data sharedpreferences SP = context.getsharedpreferences ("Jxn", Context.MODE_ PRIVATE); String number = sp.getstring ("number", null); String Password = sp.getstring ("password", null), 1.2 storage file stored in SD card://* * * StorageData into the/mnt/sdcard/jxn.txt file//Infer if there is an SD card on the current phone, assuming that 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";//generally/mnt/sdcard but different phone's SD card folder 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 the data in the Sdcard/jxn.txt file//infer 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---in the form of files

Related Article

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.