Android Learning notes-How to save data 1

Source: Internet
Author: User

Android development, sometimes we need to save information, then today to introduce, save the file to memory, as well as some of the SD card operation, and methods for reference.

First, save the data in memory:

The way in which data is saved in Java development public static Boolean saveuserinfo (String username,string password) {File File = new file ("/data/data/ Com.ftf.login/info.txt "); try {fileoutputstream fos = new FileOutputStream (file);//ftf# #123fos. Write ((username+" # # "+ Password). GetBytes ()); Fos.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return false;} return true;} Android development, the method of saving data, we pass a context object, so that you can more directly save the data to the program in the mobile phone system in a separate folder, in line with the development of Android code, public static Boolean Saveuserinfo (Context context,string username,string password) {try {File filesdir = Context.getfilesdir (); File File = new file (Filesdir, "info.txt"); FileOutputStream fos = new FileOutputStream (file);//ftf# #123fos. Write ((username+ "# #" +password). GetBytes ()); Fos.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return false;} return true;} /* * Get Saved data */public static map<string,string> Getsaveduserinfo (context context) {File Filesdir = Context.getfilesdir (); File File = new file (fileSDir, "Info.txt"); try {fileinputstream fis = new FileInputStream (file);//Use Buffer,bufferedreader BR = new BufferedReader (New InputStreamReader (FIS)); String str = br.readline (); string[] Infos = Str.split ("# #"); map<string,string> map = new hashmap<string, string> () map.put ("username", infos[0]), Map.put ("Password", INFOS[1]); return map;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}

Second, save the data to the SD card

At this time we need to use the environment, to more convenient to obtain the directory of SD card, at this time casually, the SD card is in the/data/data/sdcard directory, but some domestic mobile phones, as well as the tablet directory in the organization is not so, so as to ensure the compatibility of the program, It is also recommended for Android development specifications.

public static Boolean Saveuserinfo (Context context,string username,string password) {try {//file Filesdir = Context.getfilesdir ();//file file = new file (Filesdir, "info.txt"); if (Environment.MEDIA_MOUNTED.equals ( Environment.getexternalstoragestate ()));//Get the SD card directory file  file = new file ( Environment.getexternalstoragedirectory (), "info.txt"); FileOutputStream fos = new FileOutputStream (file);//ftf# #123fos. Write ((username+ "# #" +password). GetBytes ()); Fos.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return false;} return true;}

Third, according to the permissions, the file is stored

This is more in line with Android's development specifications, the Android file storage has four types: private,readeable,writeable,readeable+writeable, also is private, readable, writable, readable and writable, When we save the file can be specified directly, and the context can directly open a file output stream, so Android under the development of saving files, recommended this way.

public static Boolean Saveuserinfo (Context context,string username,string password,int mode) {try {////file filesdir = con Text.getfilesdir ();//file file = new file (Filesdir, "info.txt");//fileoutputstream fos = new FileOutputStream (File);// Create a file in context environment FileOutputStream FOS = Null;switch (mode) {Case 1:fos = context.openfileoutput ("Private.txt", Context.mode _private); Break;case 2:fos = Context.openfileoutput ("Readeable.txt", context.mode_world_readable); Break;case 3:fos = Context.openfileoutput ("Writeable.txt", context.mode_world_writeable); Break;case 4:fos = Context.openfileoutput (" Public.txt ", context.mode_world_readable+context.mode_world_writeable); ftf# #123fos. Write ((username+ "# #" +password). GetBytes ()); Fos.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return false;} return true;}

Android Learning notes-How to save data 1

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.