Android下私有文件夹文件的读写

私有文件夹文件的读写

对于文件的读写,我想我们并不陌生。在其它的系统平台上比如Windows系统,应用程序可以自由地或在特定的访问权限允许下进行访问或者修改其它应用程序下的文件资源;但是,在Android系统平台下却不然,一个应用程序中的所有数据都是私有的,也就是只能被自己看到的文件数据。

当应用程序被安装到Android系统之后,在系统中会生成对应的应用程序安装文件夹,用来存储应用程序的安装信息和私有的数据存储,只有这个应用程序才有权限来操作这个文件夹下的文件,这个私有文件是位于Android系统的/data/data/<应用程序包名字>目录中,其它的应用程序是没有访问或修改这个文件夹下的文件数据的。

我们应该很熟悉Java中文件流IO的文件操作流程,在Android系统中也支持Java平台下的文件I/O操作,主要是使用FileInputStreamFileOutputStream这两个方法来读取或修改文件。另外,获取上面两个文件流操作方法的途径有两种:一种就是Java平台下的实现方式,通过构造器直接构建,如果需要向打开的文件末尾写入数据,我们可以使用FileOutputStream(File file,boolean append)append设置为true来实现即可。但是需要注意的是,使用此种方法读写文件时,如果文件不存在或不能写入的话,会报出FileNotFoundException(文件找不到)的异常问题;而另一种获取方法就是在Android系统的获取方式,其实是Java平台的变本,内部原理是一样的。我们可以调用Context.openFileInputContext.openFileOutput两个方法来创建操作文件的,当然,Context对象还提供其他的集中方法来操作文件的,比如fileList(搜索应用程序私有文件夹下的所有私有文件,并返回所有文件名字的字符串数组)deleteFile(String filename),用来删除指定文件名字的私有文件,删除成功返回true,否则返回false

我们在使用Context.openFileOutput方法打开文件的时候,需要指定文件的打开模式,默认的打开模式为0,即是MODE_PRIVATE私有打开模式,下面列出其他的几种文件打开模式:

MODE_APPEND--->如果操作的文件存在则向该文件的末尾继续写入数据,而并不是类似MODE_PRIVATE模式那样覆盖原有的数据。

MODE_WORLD_READABLE--->赋予所有应用程序对这个私有文件的读操作权限。

MODE_WORLD_WRITEABLE--->赋予所有应用程序对这个私有文件的写入操作权限。

下面简单举例说明使用方法:

Main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".DataStoreAct" >

    <TextView

        android:id="@+id/file_text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"/>

    <LinearLayout 

        android:orientation="horizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/file_text"

        android:gravity="center">

        <Button 

        android:id="@+id/write_data"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/data_write"/>

        <Button 

        android:id="@+id/read_data"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/data_read"/>

    </LinearLayout>

</RelativeLayout>

DataStoreAct.java:

package com.example.datastoreprivate;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class DataStoreAct extends Activity {

public static final String ENCODING = "UTF-8";

private String fileName = "test.txt";

private String msg = "欢迎来到Android数据存储的世界!";

private TextView tv;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        tv = (TextView) findViewById(R.id.file_text);

        Button wdata = (Button) findViewById(R.id.write_data);

        Button rdata = (Button) findViewById(R.id.read_data);

        wdata.setOnClickListener(new OnClickListener() {

         public void onClick(View v) {

         //调用文件写入方法

         try {

write(fileName,msg);

} catch (IOException e) {

e.printStackTrace();

}

         }

        });

        

        rdata.setOnClickListener(new OnClickListener() {

         public void onClick(View v) {

         //调用文件读取方法

         String result = "";

         try {

result = read(fileName);

showText(result);

} catch (IOException e) {

e.printStackTrace();

}

         }

        });

        

    }

    private void showText(String val) {

     if (tv != null)

     tv.setText(val);

    }

    /*文件写入方法,将数据写入到私有文件夹文件中*/

    private void write(String fName,String msg) throws IOException {

     FileOutputStream fout = openFileOutput(fName,MODE_PRIVATE);

    

     byte[] bytes = msg.getBytes();

     fout.write(bytes);

     fout.close();

    }

    

    /*文件读取方法,从私有文件中读取数据并显示在屏幕上*/

    private String read(String fName) throws IOException{

     String result = "";

    

     FileInputStream fin = openFileInput(fName);

    

     int len = fin.available();//获取文件的长度

     byte[] buffer = new byte[len];//缓存数据

     fin.read(buffer);//从缓存中间接读取数据

     result = EncodingUtils.getString(buffer,ENCODING);

     return result;

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

    

}

程序运行的效果图如下:

下面我们继续列出使用MODE_APPEND模式操作文件的效果图(我们只需要将MODE_PRIVATE改写为MODE_APPEND即可):

好了,私有文件夹下的文件读写已经介绍完了。接下里的文章内容是《ResourcesAssets》中的文件读取。

希望兴趣相投的同学来一起研究学习!群号是:179914858 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android应用程序的私有目录是指每个应用程序在安装时会创建一个私有目录,只有该应用程序可以访问该目录中的文件,其他应用程序无法访问。私有目录通常包括应用程序的缓存目录、数据库目录和共享首选项目录等。 读取私有目录中的文件 要访问应用程序的私有目录中的文件,可以使用Context对象提供的openFileInput()方法获取一个FileInputStream对象,然后使用该对象读取文件内容。例如: ```java try { FileInputStream fis = openFileInput("file.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer); String content = new String(buffer); fis.close(); } catch (IOException e) { e.printStackTrace(); } ``` 在此示例中,我们打开名为“file.txt”的文件,并使用Java的FileInputStream类读取文件内容。我们使用fis.available()方法获取文件的大小,然后创建一个大小等于文件大小的字节数组来存储文件内容。最后,我们使用Java的String类将字节数组转换为字符串。 写入私有目录中的文件 要将数据写入应用程序的私有目录中的文件,可以使用Context对象提供的openFileOutput()方法获取一个FileOutputStream对象,然后使用该对象将数据写入文件。例如: ```java try { FileOutputStream fos = openFileOutput("file.txt", Context.MODE_PRIVATE); fos.write("Hello World".getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 在此示例中,我们打开名为“file.txt”的文件,并使用Java的FileOutputStream类将字符串“Hello World”写入文件。我们使用Context.MODE_PRIVATE参数指定文件的访问权限为私有,这意味着只有我们的应用程序可以访问该文件。 总结 Android应用程序的私有目录提供了一个安全的存储区域,只有应用程序本身才能访问该目录中的文件。通过使用Context对象提供的openFileInput()和openFileOutput()方法,我们可以读取和写入应用程序的私有目录中的文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云水之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值