Android中文件的存储与访问

1.android中数据的存储与访问(1.文件,2.SharedPreferrences(参数),3.SQLite数据库,4.内容提供者(Content provider),5.网络。五种方式)
public class FileService{
    private Context context;
    public FileService(Context context){
      this.context=context;
    }
    /**
     *以私有文件保存内容
     *@param filename 文件名称
     *@param content  文件内容
     *@throws Exception
     */
    public void save(String filename,String content)throws Exception{
      FileOutputStream outStream=context.openFileOutput(filename,Context.MODE_PRIVATE);//方法会自动生成一个文件,往文件中存放数据就要通过输出流(保存路径默认为应用所在包的files目录下)
      outStream.write(context.getBytes());//往文件中写数据(写的是二进制数据)
      outStream.close();//关闭输出流
    }
  }
  用单元测试方法对业务层进行测试:
 public class FileServiceTest extends AndroidTestCase{
    public void testSave()throws Throwable{
      FileService service = new FileService(this.getContext());//AndroidTestCase类中有方法getContext()可以得到一个上下文
    service.save("itcast.txt","www.itcast.cn");
    }
  }  
  测试无误后可以将.txt文件导出到桌面上进行查看。
  处理用户的输入事件,既响应点击事件:
fileService=new FileService(this);
  Button button=(Button)this.findViewById(R.id.button);
  button.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
      EditText filenameText=(EditText)findViewById(R.id.filename);
      EditText contentText=(EditText)findViewById(R.id.filecontent);
      String filename=filenameText.getText().toString();
      String content=contentText.getText().toString();
      try{
        fileService.save(filename,content);
        Toast.makeText(MainActivity.this,R.string.success,1).show();
      }catch(Exception e){
         Log.e(TAG,e.tostring());
         Toast.makeText(MainActivity.this,R.string.error,1).show();
       }
    }
  });
2.读取文件:
public String readFile(String filename)throws Exception{
    FileInputStream inStream=context.openFileInput(filename);
    byte[]buffer=new byte[1024];
    int len=0;
    ByteArrayOutputStream outStream = new byteArrayOutputStream();//建立内存输出流
    while((len=inStream.read(buffer))!=-1){
      outStream.write(buffer,0,len);
    }
    byte[] data=outStream.toByteArray();//得到文件的二进制数据
    outStream.close();
    inStream.close();
    return new String(data);//将二进制转换成String型
  }
3.文件的操作模式:
/**
   *以追加方式保存内容
   *@param filename文件名称
   *@param content 文件内容
   *@throws Exception
   */
  public void saveAppend(String filename,String content)throws Exception {
    FileOutputStream outStream=context.openFileOutput(filename,Context.MODE_APPEND);
    outStream.write(content.getBytes());
    outStream.close();
  }
  /**
   *保存内容,注:允许其他应用从该文件中读取内容
    *@param filename文件名称
   *@param content 文件内容
   *@throws Exception
   */
   public void saveReadable(String filename,String content)throws Exception{
     FileOutputStream outStream=context.openFileOutput(filename,context.MODE_WORLD_READABLE);
     outStream.write(content.getBytes());
     outStream.close();
   }
   /**
   *保存内容,注:允许其他应用往该文件写入内容
    *@param filename文件名称
   *@param content 文件内容
   *@throws Exception
   */
   public void saveWriteable(String filename,String content)throws Exception{
     FileOutputStream outStream=context.openFileOutput(filename,context.MODE_WORLD_READABLE);
     outStream.write(content.getBytes());
     outStream.close();
   }
    所创建的模式智能被创建者所访问,其他应用无法访问,在这里我们可以建立一个类测试一下。
    public void testAccessOtherAppFile()thows Throwable{
    String path = "/data/data/cn.itcast.file/files/append.txt";//若要访问首先的要知道访问对象的路径
    File file= new File(path);//读取文件要传入路径
    FileInputStream inStream=new FileInputStream(file);//建立输入流对象
    byte[]buffer=new byte[1024];
    int len=0;
    ByteArrayOutputStream outStream=new ByteArrayOutputStream();
    while((len=inStream.read(buffer))!=-1){
      outStream.write(buffer,0,len);
    }
    byte[]data=outStream.toByteArray();//得到文件的二进制数据
    outStream.close();
    instream.close();
    Log.i(TAG,new String(data));
  }
    通过saveReadable我们可以让其他应用进行访问:
  public void testAccessOtherAppReadable()thows Throwable{
    String path = "/data/data/cn.itcast.file/files/readable.txt";//若要访问首先的要知道访问对象的路径
    File file= new File(path);//读取文件要传入路径
    FileInputStream inStream=new FileInputStream(file);//建立输入流对象
    byte[]buffer=new byte[1024];
    int len=0;
    ByteArrayOutputStream outStream=new ByteArrayOutputStream();
    while((len=inStream.read(buffer))!=-1){
      outStream.write(buffer,0,len);
    }
    byte[]data=outStream.toByteArray();//得到文件的二进制数据
    outStream.close();
    instream.close();
    Log.i(TAG,new String(data));
  }
4.Activity生命周期总结:
  a.Activity从创建到进入运行状态所触发的事件:onCreate()->onStart()->onResume()。
  b.当Activity从运行状态到停止状态所触发的事件:onPause()->onStop()。
  c.当Activity从停止状态到运行状态所触发的事件:onRestart()->onStart()->onResume()。
  d.当Activity从运行状态到暂停状态所触发的事件:onPause()。
  e.当Activity从暂停状态到运行状态所触发的事件:onResume()。
 生命周期事件只有Activity和Activity之间转换时才会触发。
 判断是否是Activity生命周期触发事件,可以在运行程序前,先看看AndroidManifest.xml中注册的Activity,只有他们之间相互跳转时才会触发。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值