废话少说,直接上代码,看不懂的地方自己去网上查,以下代码都在我本地运行正常:
新建一个FileRW的android的项目:
新建一个com.sharandroi.file包
新建一个FileService.java的具体操作类:
package com.sharandroi.file;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
this.context = context;
}
public void save(String content) throws Exception {
FileOutputStream outStream = context.openFileOutput("sharpandroid.txt",
Context.MODE_WORLD_WRITEABLE);
outStream.write(content.getBytes());// 写数据
outStream.close();// 关闭输出流
}
public String read() throws Throwable{
FileInputStream instream = context.openFileInput("sharpandroid.txt");
byte[] data = readFile(instream);
return new String(data);
}
public byte[] readFile(InputStream inStream) throws Throwable{
int len = 0;
byte[] buffer = new byte[1024];
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while((len = inStream.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
outStream.close();
return outStream.toByteArray();
}
}
新建一个单元测试类:FileServiceTest.java
package com.sharandroi.file;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
public class FileServiceTest extends AndroidTestCase {
public void testSave() throws Throwable{
FileService fileService = new FileService(getContext());
fileService.save("qqqqqqqqqqqqqqqqqqqqqqqqqq");
Log.i("aaa", fileService.read());
}
}
AndroidManifest.xml配置添加权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sharandroi.file" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
<uses-sdk android:minSdkVersion="8" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.sharandroi.file" android:label="Tests for My App" />
</manifest>
上面的代码把需要用的包都导进来在我本地能跑起来
File Exploreer的下面看到data/data/com.sharandroi.file/下面有个sharpandroid.txt文件倒出来就可以看到内容了
分享到:
相关推荐
Android - 文件读写操作 方法总结
Android文件读写操作总结
android 通过Environment环境 获取手机根目录 读写 文件操作。
主要介绍了Android 文件读写操作方法总结的相关资料,需要的朋友可以参考下
Android SD卡文件读写操作可以学习下
Android 读写文件实例,包括 SD 卡读写和 内部存储读写。 文件目录获取等。
C#中的文件读写详细操作,详细介绍了文件读写的操作。
android开发的文件读写,简单操作。适合初学者参考。
实时检测USB的插入,检测并读取文件,也可根据路径进行文件读写操作
Android jni文件读写 NDK文件读取 读写 操作 NDK数据加密DEMO 源码直接上传,运行在android6.0系统测试通过
android文件读写源代码,支持系统文件读取
android 读写文件的工具类,对读写操作进行了封装。
Android读写文件,该文件包含Android里的读写txt文件,方便初学者使用同时欢迎大家交流经验@!
android文件的创建、删除、 读写、 判断是否操作、文件MD5 java基础学习型
里面有一些简单的http请求方法,及如何在读写文件到SD卡中,仅供参考! 本人觉得应该还是挺有用的!
本文实例总结了Android文件读写操作。分享给大家供大家参考,具体如下: 在Android中的文件放在不同位置,它们的读取方式也有一些不同。 本文对android中对资源文件的读取、数据区文件的读取、SD卡文件的读取及...
c语言文件读写操作代码
Android_SDCard操作(文件读写,容量计算)Android_SDCard操作(文件读写,容量计算)
android的IO流读写文件操作示意程序