第一步,改写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; } }
第三部,编写测试类进行单元测试,进行文件的读写
1 package com.example.androidsdcard; 2 3 import android.content.Context; 4 import android.test.AndroidTestCase; 5 import android.util.Log; 6 7 public class MyTest extends AndroidTestCase { 8 private final String TAG="MyTest"; 9 public MyTest(){ 10 11 } 12 public void saveFile(){//测试文件写入 13 Context context=getContext(); 14 FileStream fileStream=new FileStream(context); 15 boolean flag=fileStream.savaFile("hello.txt","你好吗?"); 16 Log.i(TAG,"--->"+flag); 17 18 } 19 public void readFile(){//测试文件内容读取 20 Context context=getContext(); 21 FileStream fileStream=new FileStream(context); 22 String filename="hello.txt"; 23 String flag=fileStream.readFile(filename); 24 Log.i(TAG,"--->"+flag); 25 26 } 27 28 }