Sdcard read/write data for Android Development (source code sharing)

Source: Internet
Author: User

Package com. example. f01_sdcard01; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import android. OS. environment; public class FileUtiles {// when writing data to the SD card, remember to add a <uses-permission // android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> // pass two parameters. One is the file name and the other is the written data public void fileSave (String fileName, byte [] data) {FileOutpu TStream fileOutputStream = null; String state = Environment. getExternalStorageState (); if (Environment. MEDIA_MOUNTED.equals (state) {// create the sdcard directory File = new file (Environment. getExternalStorageDirectory (). getAbsolutePath () + "/txt"); // if the directory does not exist, create a file if (! File. exists () {file. mkdirs ();} try {// write data to fileOutputStream = new FileOutputStream (new File (file, fileName) in the specified File; try {fileOutputStream. write (data, 0, data. length);} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (fileOutputStream! = Null) {try {fileOutputStream. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}}} public String readSdcard (String fileName) {// read the data of the specified file
If (Environment. MEDIA_MOUNTED.equals (Environment. getExternalStorageState () {File file = new File (Environment. getExternalStorageDirectory (). getAbsolutePath () + "/txt/"); // if the directory does not exist, create a file if (file. exists () {File file2 = new File (file, fileName); InputStream inputStream = null; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream (); try {inputStream = new FileInputStream (file2 ); byte [] data = ne W byte [1024]; int len = 0; try {while (len = inputStream. read (data ))! =-1) {arrayOutputStream. write (data, 0, len);} return new String (arrayOutputStream. toByteArray ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (inputStream! = Null) {try {inputStream. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. 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.