总的来说向内存和SDcard中读写数据和java io操作基本差不多 而机身内存读取和SDcard读取数据有少许差别
机身内存数据读写
1.机身内存读取主要用个两个类文件输入流(FileInputStream)和文件输出流(FileOutputStream): FileInputStream fileInput = this.openFileInput("test.txt") 第一个参数为 data/此程序包名/data/test.txt 文件下 的文件名 ;
FileOutputStream fileOut = this.openFileOutput("test.txt",this.MODE_APPEND)第一个参数表示文件名 第二个参数表示打开的方式
2.获取了文件输入输出流之后 其后的文件的读写和基本的IO操作一样
SDcard数据读写
1.SDcard数据读写需要注定的先要在Androidmainfest.xml文件中注册新建删除和读写的权限 :
<!-- 在SD卡上创建与删除权限 -->
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
<!-- 向SD卡上写入权限 -->
<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可以导出来
2.3 其后就和基本IO操作相同了
2.4还有要注意一点的是 在运行的模拟器的时候要附带虚拟的SDcard时 要在Run as->Run Configurations 中要关联一下 如下图

机身内存数据读写实例
- <LinearLayout xmlns:android="http://schemas./apk/res/android"
- xmlns:tools="http://schemas./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数据读写实例
- <LinearLayout xmlns:android="http://schemas./apk/res/android"
- xmlns:tools="http://schemas./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./apk/res/android"
- package="com.android.xiong.sdcardtest"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="17" />
- <!-- 在SD卡上创建与删除权限 -->
- <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
- <!-- 向SD卡上写入权限 -->
- <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>
|