Android data file access to the storage card, android access
From: http://blog.csdn.net/jianghuiquan/article/details/8569233
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Button android: id = "@ + id/save" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "File"/> <Button android: id = "@ + id/read" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "File"/> </LinearLayout>
Package com. example. yanlei. wifi; import android. OS. bundle; import android. OS. environment; import android. support. v7.app. appCompatActivity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. printStr Eam; import java. util. extends; public class MainActivity extends AppCompatActivity {private Button btnSave = null; private Button btnRead = null; private File file File = null; private static final String FILENAME = "data.txt "; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnSave = (Button) super. findViewById (R. id. save ); BtnRead = (Button) super. findViewById (R. id. read); btnSave. setOnClickListener (new OnClickListener () {public void onClick (View v) {PrintStream ps = null; // determine whether an external memory card exists if (! Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {Toast. makeText (getApplicationContext (), "Read failed, SD memory card does not exist! ", Toast. LENGTH_LONG ). show (); return;} // initialize File String path = Environment. getExternalStorageDirectory (). toString () + File. separator + "genwoxue" + File. separator + FILENAME; file = new File (path); // if the parent folder of the current file does not exist, create the genwoxue folder if (! File. getParentFile (). exists () file. getParentFile (). mkdirs (); // write the file try {ps = new PrintStream (new FileOutputStream (file); ps. println (": www.genwoxue.com"); ps. println (""); ps. println ("Email: hello@genwoxue.com");} catch (FileNotFoundException e) {e. printStackTrace ();} finally {ps. close ();} Toast. makeText (getApplicationContext (), "saved successfully! ", Toast. LENGTH_LONG ). show () ;}}); btnRead. setOnClickListener (new OnClickListener () {public void onClick (View v) {StringBuffer info = new StringBuffer (); // determine whether an external memory card exists if (! Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {Toast. makeText (getApplicationContext (), "Read failed, SD memory card does not exist! ", Toast. LENGTH_LONG ). show (); return;} // initialize File String path = Environment. getExternalStorageDirectory (). toString () + File. separator + "genwoxue" + File. separator + FILENAME; file = new File (path); if (! File. exists () {Toast. makeText (getApplicationContext (), "the file does not exist! ", Toast. LENGTH_LONG ). show (); return;} // read the file's content using scan = null; try {scan = new external (new FileInputStream (file); while (scan. hasNext () {info. append (scan. next ()). append ("☆☆\ n");} Toast. makeText (getApplicationContext (), info. toString (), Toast. LENGTH_LONG ). show ();} catch (FileNotFoundException e) {e. printStackTrace ();} finally {scan. close ();}}});}}
Permission
<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. example. yanlei. wifi"> <! -- Create and delete file permissions in SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Write data permission to SDCard --> <uses-permission android: name = "android. permission. READ_EXTERNAL_STORAGE "/> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "/> <application android: allowBackup =" true "android: icon =" @ mipmap/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />