Reading and Writing files in android and reading and writing files in android
Read and Write files in android
There is only one disk in android, and the forward slash/Represents the root directory.
The common SDK is/mnt/sdcard.
Two common data storage methods:
I. Memory
Ii. Local
1. Mobile phone internal storage
2. External Storage Device (SD card)
You do not need permission to read data on the SD card, but you need permission to write data on the SD card:
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
The method for reading and writing files is to use the Java file input stream and output stream, which is almost the same as the method for reading and writing files in java.
Code:
Com. example. readwrite. MainActivity
1 package com. example. readwrite; 2 3 import java. io. file; 4 import java. io. fileInputStream; 5 import java. io. fileOutputStream; 6 import java. io. IOException; 7 8 import android. app. activity; 9 import android. OS. bundle; 10 import android. OS. environment; 11 import android. util. log; 12 13/** 14 * The forward slash represents the two most common data storage methods in the root directory: 15*16*1. Memory 2. Local 1. mobile phone internal storage 2. external Storage Device (SD card) 17 **/18 public class MainActivity exte Nds Activity {19 20 @ Override 21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 // existSDcard (); 25 // write (); 26 // listPath (); 27 read (); 28} 29 30 private void write () {31 // mnt/sdcard 32 File file = Environment. getExternalStorageDirectory (); 33 FileOutputStream out = null; 34 try {35 out = new Fi LeOutputStream (file. getPath () + "/bihu.txt"); 36 // out = new FileOutputStream (37 // "/data/com. example. readwrite/bihu.txt "); 38 out. writes ("12345 ". getBytes (); 39} catch (IOException e) {40 e. printStackTrace (); 41} finally {42 if (out! = Null) {43 try {44 out. close (); 45} catch (IOException e) {46 // TODO Auto-generated catch block 47 e. printStackTrace (); 48} 49} 50} 51} 52 53 private void read () {54 FileInputStream in = null; 55 try {56 // in = new FileInputStream ("/mnt/sdcard/bihu.txt"); 57 in = new FileInputStream (58 "/data/com. jiguang. test/databases/rep. db "); 59 byte [] bytes = new byte [2014]; 60 int len = in. r Ead (bytes); 61 String str = new String (bytes, 0, len); 62 Log. d ("bihu", "---------" + str); 63} catch (IOException e) {64 Log. d ("bihu", "error" + e. toString (); 65} finally {66 if (in! = Null) {67 try {68 in. close (); 69} catch (IOException e) {70 e. printStackTrace (); 71} 72} 73} 74} 75 76/** 77 * check whether the SD card is mounted 78 **/79 private void existSDcard () {80 // get the SD card status 81 String state = Environment. getExternalStorageState (); 82 83 if (Environment. MEDIA_MOUNTED.equals (state) {84 Log. d ("bihu", "SD card available"); 85} else {86 Log. d ("bihu", "No SD card"); 87} 88} 89 90/** 91 * obtain path 92 through API **/93 private void listPath () {94 // get the SD card directory 95 File file1 = Environment. getExternalStorageDirectory (); 96 Log. d ("bihu", "SD card ----" + file1.getPath (); 97 // get the file directory 98 File file2 = getFilesDir (); 99 Log. d ("bihu", "internal storage File ----" + file2.getPath (); 100 // obtain the cache directory of the internal storage space 101 File file3 = getCacheDir (); 102 Log. d ("bihu", "internal storage cache directory ----" + file3.getPath (); 103} 104}
Main Interface
/Read and write local files/AndroidManifest. xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <manifest xmlns: android = "http://schemas.android.com/apk/res/android" 3 package = "com. example. readwrite "4 android: versionCode =" 1 "5 android: versionName =" 1.0 "> 6 7 <! -- Write permission on the external device (SD card) --> 8 <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> 9 10 <uses-sdk11 android: minSdkVersion =" 8 "12 android: targetSdkVersion =" 19 "/> 13 14 <application15 android: allowBackup =" true "16 android: icon = "@ drawable/ic_launcher" 17 android: label = "@ string/app_name" 18 android: theme = "@ style/AppTheme"> 19 <activity20 android: name = ". mainActivity "21 android: label =" @ string/app_name "> 22 <intent-filter> 23 <action android: name =" android. intent. action. MAIN "/> 24 25 <category android: name =" android. intent. category. LAUNCHER "/> 26 </intent-filter> 27 </activity> 28 </application> 29 30 </manifest>
Configuration File