Android–sdcard file Read and save

Source: Internet
Author: User

Background

Some things can exist in their own definition of the file inside, this file can be in the phone, can be in the SD card, here is the main introduction of the SD card storage and reading it ~

Code

 Public classSave { Public Static voidSavefile2card (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 =NewFile (Environment.getexternalstoragedirectory (), "Info.txt"); FOS=Newfileoutputstream (file); Fos.write ((username+"!!!!" +password). getBytes ()); }            Else{toast.maketext (context,"SD Wood has", Toast.length_long). Show (); }        }         Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); Toast.maketext (Context,"Wrong", Toast.length_long). Show (); Try{fos.close (); }             Catch(IOException E1) {//TODO Auto-generated catch blockE1.printstacktrace (); }        }
    }}

The above is the stored code, where the environment.media_mountedis used to see if the mount.

 Public classRead { Public StaticMap<string,string>Getsavefile (Context context) {//File File =new file (Context.getfilesdir (), "Info.txt");File File =NewFile (Environment.getexternalstoragedirectory (), "Info.txt"); Try{FileInputStream fis=Newfileinputstream (file); BufferedReader BR=NewBufferedReader (NewInputStreamReader (FIS)); String Str=Br.readline (); String[] Infos= Str.split ("!!!!"); Map<String,String> map =NewHashmap<string, string>(); Map.put ("Username", infos[0]); Map.put ("Password", infos[1]);            Br.close (); returnmap; }         Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); return NULL; }        finally        {                    }            }}

The above is read code, will be saved will read, stored in so read out scatter ~

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Sdcard.zip

Reprint Please specify 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.