第一步,改写AndroidManifest.xml,给手机的Scard卡授权
<!--添加Scard卡读写授权 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后加单元测试instrumentation
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.androidsdcard" >
</instrumentation>
并且在application下加 <uses-library android:name="android.test.runner"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner"/>
<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>
第二步,编写文件读写的类FileStream
package com.example.androidsdcard;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.content.Context;
import android.os.Environment;
public class FileStream {
private Context context;
public FileStream(Context context) {
this.context = context;
}
public FileStream() {
}
//读取sd卡的文件内容
public String readFile(String filename){
FileInputStream inputStream=null;
//字符缓冲流
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
// 通过Environment获得sd卡的路径
File file=new File(Environment.getExternalStorageDirectory(),filename);
//判断sdcard是否存在
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
try {
inputStream=new FileInputStream(file);
int len=0;
//创建字节数组
byte[]data=new byte[2048];
//按字节数组的大小进行读取
while((len=inputStream.read(data))!=-1){
outputStream.write(data, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return new String(outputStream.toByteArray());
}
public boolean savaFile(String filename, String content) {
boolean flag = false;
FileOutputStream outputStream = null;
File file = new File(Environment.getExternalStorageDirectory(),
filename);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
try {
outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
flag = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return flag;
}
}
第三部,编写测试类进行单元测试,进行文件的读写
package com.example.androidsdcard;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
public class MyTest extends AndroidTestCase {
private final String TAG="MyTest";
public MyTest(){
}
public void saveFile(){//测试文件写入
Context context=getContext();
FileStream fileStream=new FileStream(context);
boolean flag=fileStream.savaFile("hello.txt","你好吗?");
Log.i(TAG,"--->"+flag);
}
public void readFile(){//测试文件内容读取
Context context=getContext();
FileStream fileStream=new FileStream(context);
String filename="hello.txt";
String flag=fileStream.readFile(filename);
Log.i(TAG,"--->"+flag);
}
}