灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3355回复:0

android学习笔记6----------数据的存储与访问(1)

楼主#
更多 发布于:2012-09-06 13:44


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                   只能被创建文件的应用程序读写,同名追加内容
3.Context.MODE_WORLD_READEBLE =1      能被其他应用程序读取
4.Context.MODE_WORLD_WRITEABLE =2     能被其他应用程序写入


执行测试单元后可以看到:(前提配置好测试单元)



将123.txt传到桌面
打开可以看到




摘自 奔跑的蜗牛


喜欢0 评分0
游客

返回顶部