android学习笔记6----------数据的存储与访问(1)
android为数据存储提供了多种方式:
1.文件
2.SharedPreferences (主要存储软件的配置参数)
3.SQLite数据库
4.内容提供者(content provider)(对外共享数据时使用)
5.网络
首先看对文件的操作,实现的要求:
1.指定保存的文件类型
2.指定保存的文件文本信息
3.读取指定的文件内容
一、业务bean
[java]
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileService
{
/**
* 保存数据
*
* @param stream
* @param content
* @throws IOException
*/
public static void save(OutputStream stream, String content)
throws IOException
{
stream.write(content.getBytes());
stream.close();
}
/**
* 读取数据
*
* @param inStream
* @return
* @throws IOException
*/
public static String read(InputStream inStream) throws IOException
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = inStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
}
byte[] data = outputStream.toByteArray();
inStream.close();
outputStream.close();
return new String(data);
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileService
{
/**
* 保存数据
*
* @param stream
* @param content
* @throws IOException
*/
public static void save(OutputStream stream, String content)
throws IOException
{
stream.write(content.getBytes());
stream.close();
}
/**
* 读取数据
*
* @param inStream
* @return
* @throws IOException
*/
public static String read(InputStream inStream) throws IOException
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = inStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
}
byte[] data = outputStream.toByteArray();
inStream.close();
outputStream.close();
return new String(data);
}
}
二、单元测试
[java]
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
import com.luku.file.service.FileService;
public class FileServiceTest extends AndroidTestCase
{
private static final String TAG = "FileServiceTest";
/**
* 保存测试单元
* 保存在/data/data/包名/files下
*
* @throws Exception
*/
public void testSave() throws Exception
{
OutputStream stream = this.getContext().openFileOutput("123.txt",//文件名称为123.txt
Context.MODE_PRIVATE);
FileService.save(stream, "你好"); //文件内容
}
/**
* 读取测试单元
* @throws Exception
*/
public void testRead() throws Exception
{
InputStream instream = this.getContext().openFileInput("123.txt");
String string = FileService.read(instream);
Log.i(TAG, string);
}
}
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
import com.luku.file.service.FileService;
public class FileServiceTest extends AndroidTestCase
{
private static final String TAG = "FileServiceTest";
/**
* 保存测试单元
* 保存在/data/data/包名/files下
*
* @throws Exception
*/
public void testSave() throws Exception
{
OutputStream stream = this.getContext().openFileOutput("123.txt",//文件名称为123.txt
Context.MODE_PRIVATE);
FileService.save(stream, "你好"); //文件内容
}
/**
* 读取测试单元
* @throws Exception
*/
public void testRead() throws Exception
{
InputStream instream = this.getContext().openFileInput("123.txt");
String string = FileService.read(instream);
Log.i(TAG, string);
}
}
补充:
openFileOutput第二个参数有四种模式,用于控制访问权限,分别为:
1.Context.MODE_PRIVATE =0 只能被创建文件的应用程序读写,同名覆盖
2.Context.MODE_APPEND =32768 &nbs
补充:移动开发 , Android ,