Android-SDcard File Reading and saving

Source: Internet
Author: User

Background

Some things can be stored in your own defined file. This file can be stored on your mobile phone or on the SD card. Here we will mainly introduce the storage and reading of things on the SD card ~

Code

Public class save {public static void savefile2card (Context context, String username, String password) {File file = null; FileOutputStream fos = null; try {if (Environment. MEDIA_MOUNTED.equals (Environment. getExternalStorageState () {// file = new File ("/sdcard/info.txt"); file = new File (Environment. getExternalStorageDirectory (), "info.txt"); fos = new FileOutputStream (file); fos. write (username + "! !!! "+ Password ). getBytes ();} else {Toast. makeText (context, "SD Wood", Toast. LENGTH_LONG ). show () ;}} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); Toast. makeText (context, "Wrong", Toast. LENGTH_LONG ). show (); try {fos. close ();} catch (IOException e1) {// catch Block e1.printStackTrace () generated automatically by TODO ();}}
    }}

The above is the stored code, where Environment. MEDIA_MOUNTED is used to check whether mounting is performed.

Public class read {public static Map <String, String> getSaveFile (Context context) {// File file = new File (context. getFilesDir (), "info.txt"); File file = new File (Environment. getExternalStorageDirectory (), "info.txt"); try {FileInputStream FCM = new FileInputStream (file); BufferedReader br = new BufferedReader (new InputStreamReader (FCM); 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]); br. close (); return map;} catch (Exception e) {// catch Block e automatically generated by TODO. printStackTrace (); return null ;}finally {}}}

The above is the read code. It will be saved and read. If it is saved, it can be read out ~

I am the dividing line of tiantiao

Source code: http://pan.baidu.com/s/1dD1Qx01

SDcard.zip

 

 

 

Reprinted please indicate the source: http://www.cnblogs.com/yydcdut/p/3720648.html

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.