android学习笔记(4)-android的文件的操作模式与单元测试

android的文件的操作模式

android的文件操作有四个模式

openFileOutput(String name, int mode)//第一个参数表示文件名称,第二个参数表示文件模式

如下事例

FileOutputStream out = context.openFileOutput(filePath, Context.MODE_PRIVATE);//context是上下文对象


1.私有操作模式    Context.MODE_PRIVATE

该操作模式下,创建出来的文件仅能被本应用访问,其他应用无法访问该文件,另外私有模式创建的文件,写入的内容会覆盖原文件内容。


2.追加操作模式    Context.MODE_APPEND
该操作模式下,创建的文件仅能被本应用访问,其他应用无法访问该文件,但是该模式会检查文件是否存在,若存在,则会在文件后面加上新内容,否则,创建新文件。


3.Context.MODE_READABLE
该模式下,创建的的文件可以被其他应用读取。


4.Context.MODE_WRITEABLE

该模式下,创建的文件可以被其他应用写入数据。(不能读取)

Context.MODE_READABLE+Context.MODE_WRITEABLE 表示读和写。

下面是四种模式的值

Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2





单元测试

要测试的代码为如下


public String read(String filePath) throws Exception{
FileInputStream in = context.openFileInput(filePath);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while((count = in.read(buffer)) != -1){
outStream.write(buffer, 0, count);
}
byte[] date = outStream.toByteArray();
return new String(date);

}


1. 搭建单元测试的环境 在Mainfest.xml中加入<uses-library android:name="android.test.runner"/> (在Application节点下)和
<instrumentation android:name="android.test.InstrumentationTestRunner"

    android:targetPackage="com.example.File" android:label="Tests for My App"/>(在manifest节点下)

android:targetPackage 表示要测试的目标文件夹


如下所示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.File"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
	
    <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="com.example.File.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>
<instrumentation android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.example.File" android:label="Tests for My App"/>
</manifest>

2.建立单元测试类 
该类必须继承AndroidTestCase

public class ServiceTest extends AndroidTestCase {
private static final String Tag = "ServiceTest";
public void testRead() throws Throwable{
FileService service = new FileService(this.getContext());
String text = service.read("adfkjhg.txt");
Log.i(Tag, text);
}
}

3.运行为Android Junit Test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值