Read and write files under Android

Source: Internet
Author: User

Save the file

public static Boolean Saveuserinfo (context context, string Username, string password) {try{//defines a file path object. E (Context.getfilesdir (), "info.txt");//defines the write stream object for a file FileOutputStream fos = new FileOutputStream (file);// Write the data to the file with the write stream object Fos.write ((username + "# #" + password). GetBytes ());//write stream for closed file Fos.close (); return true;} catch (Exception e) {e.printstacktrace (); return false;}}

Read the file

public static map<string, string> Getsaveduserinfo (context context) {try{//defines a file path object. Context.getfilesdir (), "info.txt");//define a read stream object for a file Fisfileinputstream fis = new FileInputStream (file);// A read stream object that defines a character brbufferedreader br = new BufferedReader (FIS);//reads a row of data in a text file string = Br.readline ();//Use the Split method style string to save the split string data to a string array string[] Infos = String.Split ("# #");//define a map collection, Used to save the segmented string array information map<string, string> map = new hashmap<string, string> (), Map.put ("username", infos[0]); Map.put ("Password", infos[1]); return map;} catch (Exception e) {e.printstacktrace (); return null;}}


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.