Android數(shù)據(jù)存儲(2)向機身內(nèi)存(Internal Storage)和SDcard(External Storage)讀寫數(shù)據(jù)實例
總的來說向內(nèi)存和SDcard中讀寫數(shù)據(jù)和java io操作基本差不多 而機身內(nèi)存讀取和SDcard讀取數(shù)據(jù)有少許差別
機身內(nèi)存數(shù)據(jù)讀寫
1.機身內(nèi)存讀取主要用個兩個類文件輸入流(FileInputStream)和文件輸出流(FileOutputStream): FileInputStream fileInput = this.openFileInput("test.txt") 第一個參數(shù)為 data/此程序包名/data/test.txt 文件下 的文件名 ;
FileOutputStream fileOut = this.openFileOutput("test.txt",this.MODE_APPEND)第一個參數(shù)表示文件名 第二個參數(shù)表示打開的方式
2.獲取了文件輸入輸出流之后 其后的文件的讀寫和基本的IO操作一樣
SDcard數(shù)據(jù)讀寫
1.SDcard數(shù)據(jù)讀寫需要注定的先要在Androidmainfest.xml文件中注冊新建刪除和讀寫的權(quán)限 :
<!-- 在SD卡上創(chuàng)建與刪除權(quán)限 -->
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
<!-- 向SD卡上寫入權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.讀寫的基本流程就是:
2. 1 通過Environment類的getExternalStorageState()方法來判斷手機是否有SDcard: Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
2.2 最通過getExternalStorageDirectory()方法來獲取文件目錄:File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "/test.txt"); 讀寫的文件都在sdcrad文件夾中 通過File Explorer可以導(dǎo)出來
2.3 其后就和基本IO操作相同了
2.4還有要注意一點的是 在運行的模擬器的時候要附帶虛擬的SDcard時 要在Run as->Run Configurations 中要關(guān)聯(lián)一下 如下圖
機身內(nèi)存數(shù)據(jù)讀寫實例
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="center_horizontal"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/ed1"
- android:inputType="textMultiLine"/>
- <Button
- android:id="@+id/write"
- android:text="寫入"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/read"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="讀入"/>
- <EditText
- android:id="@+id/ed2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textMultiLine"/>
- <Button
- android:id="@+id/delete"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="刪除指定的文件"
- />
- <EditText
- android:id="@+id/ed3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
-
- </LinearLayout>
SDcard數(shù)據(jù)讀寫實例
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <EditText
- android:id="@+id/ed1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textMultiLine"/>
- <Button
- android:id="@+id/write"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="寫入SD卡中"/>
- <Button
- android:id="@+id/read"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="讀取SD文件"/>
- <TextView
- android:id="@+id/txt1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.xiong.sdcardtest"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="17" />
- <!-- 在SD卡上創(chuàng)建與刪除權(quán)限 -->
- <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
- <!-- 向SD卡上寫入權(quán)限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.android.xiong.sdcardtest.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>
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。