android 提供的5种数据存储方式
文件(Java SE中的IO技术)
SharedPreferences(参数)
SQlite数据库
Content Provider(内容提供者)
网络
1.文件存储方式
IO技术简单介绍:
数据流的划分:输入流(InputStream)和输出流(OuputStream)[按留香划分];;;字符流(如FileReadr,FileWriter,BufferedReader,BufferedWriter)和字节流(如FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream)[按数据内容划分]
注意:当从内存向文件中写入数据位输出流,从文件中读取数据到内存为输入流(进内存为输入,出内存为输出)
andorid存储空间:android自带的存储空间,自带存储空间较小(类似电脑自带的硬盘)和外存储设备SDCard(类似U盘等)一般外存储设备比自带存储空间大
2.保存文件的代码(这里为保存数据到手机自带的存储空间)
/**
* 保存文件
* @param filename 文件名
* @param content 文件内容
* @throws Exception 抛出异常
*/
public class FileService {
private Context context;
public FileService(Context context){
this.context=context;
}
public void save(String filename, String content) throws Exception {
//私有操作模式,创建的文件只能被应用本身访问,其他应用无法访问,写入的文件覆盖原文件内容
OutputStream outStream=context.openFileOutput(filename, context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
}
openFileOutput的说明:
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
//打开一个用来写文件的与当前上下文的应用的包相关私有文件,如果文件不存在就创建新的
Parameters
//文件名不能包含路径,因为文件保存在默认的文件路径下(与应用的包名有关,见下文文件的保存位置图片)
name | The name of the file to open; can not contain path separators. |
---|---|
mode | Operating mode. Use 0 or MODE_PRIVATE for the default operation,MODE_APPEND to append to an existing file,MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE to control permissions. |
Returns
- FileOutputStream Resulting output stream.
对于文件操作模式Context.MODE_*的解释:
MODE_PRIVATE 私有模式:为默认的操作模式,文件为私有数据,只能被应用自身访问,该模式下写入的文件会覆盖原文件
MODE_APPEND 追加模式:文件存在,追加内容,文件不存在则创建新文件
MODE_WORLD_READABLE 只读模式
MODE_WORLD_WIRTEABLE 只写模式
实现可读写方法:
在openFileOutput方法中的mode参数中传人"MODE_WORLD_READABLE+MODE_WORLD_WIRTEABLE"
MainActivity部分:
public class AndroidIOActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et=(EditText) findViewById(R.id.filname);
EditText etc=(EditText) findViewById(R.id.content);
String filename=et.getText().toString();
String content=etc.getText().toString();
FileService service=new FileService(getApplicationContext());
try {
service.save(filename, content);
Toast.makeText(getApplicationContext(), "保存成功", 1).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "保存失败", 1).show();
}
}
});
}
}
运行界面及结果:
文件保存的默认位置:
/data/data/org.wx.test(应用的包名)/files/wx.txt(文件名)
3.读取文件的代码
/**
* 从文件中读取数据
* @param filename文件名
* @return
* @throws Exception 抛出异常
*/
public String read(String filename) throws Exception{
InputStream in=context.openFileInput(filename);
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
return new String(out.toByteArray());
}
4.使用单元测试测试
manifest引入类库和增加权限
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidIOActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 单元测试 -->
<uses-library android:name="android.test.runner" />
</application>
<!-- 单元测试 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="org.wx.test" />
extends AndroidTestCase 类代码:
public class FileServiceTest extends AndroidTestCase {
private static final String TAG="FILE_TEST_LOG";
public void testRead() throws Throwable{
FileService fs=new FileService(this.getContext());
String result=fs.read("wx.txt");
Log.i(TAG, result);
}
}
单元测试运行结果:
日志记录结果: