android的文件的操作模式
openFileOutput(String name, int mode)//第一个参数表示文件名称,第二个参数表示文件模式
如下事例
FileOutputStream out = context.openFileOutput(filePath, Context.MODE_PRIVATE);//context是上下文对象
1.私有操作模式 Context.MODE_PRIVATE
该模式下,创建的文件可以被其他应用写入数据。(不能读取)
下面是四种模式的值
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);
}
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>
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);
}
}