android 内部文件读取

Android 文件管理方法

Android使用的是基于Linux的文件系统,对于文件的访问和管理是通过权限设置来限制的.

在Linux系统中,文件权限分别描述了创建者、同组用户和其他用户对文件的操作限制。

x表示可执行,r表示可读,w表示可写,d表示目录,-表示普通文件。

产生这样的文件权限与程序人员设定的

Android 存储文件的类型

(内部存储)程序开发人员可以建立和访问程序自身的私有文件;

(资源存储)可以访问保存在资源目录中的原始文件和XML文件;

(外部存储)可以在SD卡等外部存储设备中保存文件

Android系统允许应用程序创建仅能够自身访问的私有文件,文件保存在设备的内部存储器上,在Linux系统下的/data/data//files目录中

Android系统不仅支持标准Java的IO类和方法,还提供了能够简化读写流式文件过程的函数

FileOutputStream openFileOutput(String filename int mode)

FileInputStream openFileInput(String filename)

参数文件不允许包含描述路径的斜杠(其存储位置固定)

访问模式 :

MODE_PRIVATE 私有模式,缺陷模式,文件仅能够被文件创建程序访问,或具有相同UID的程序访问

MODE_APPEND 追加模式,如果文件已经存在,则在文件的结尾处添加新数据。

MODE_WORLD_READABLE 全局读模式,允许任何程序读取私有文件。

MODE_WORLD_WRITEABLE 全局写模式,允许任何程序写入私有文件。

三个基本的读方法

abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。

int read(byte[] b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。

int read(byte[] b, int off, int len) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
其它方法

long skip(long n) :在输入流中跳过n个字节,并返回实际跳过的字节数。

int available() :返回在不发生阻塞的情况下,可读取的字节数。

void close() :关闭输入流,释放和这个流相关的系统资源。

void mark(int readlimit) :在输入流的当前位置放置一个标记,如果读取的字节数多于readlimit设置的值,则流忽略这个标记。
void reset() :返回到上一个标记。

boolean markSupported() :测试当前流是否支持mark和reset方法。如果支持,返回true,否则返回false。

三个基本的写方法

abstract void write(int b) :往输出流中写入一个字节。

void write(byte[] b) :往输出流中写入数组b中的所有字节。

void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。

其它方法

void flush() :刷新输出流,强制缓冲区中的输出字节被写出。

void close() :关闭输出流,释放和这个流相关的系统资源。

对文件和流的操作容易引发异常,所以必须要用try-catch语句

主要核心代码

首先是新建一个文件

private final String FILE_NAME = "Myfile01.txt";

写文件
  1. FileOutputStream fos = null;//声明一个全局变量
  2. //注意下面的语句要进行抛异常处理FileNotFoundException e,IOException e
  3. fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);//写流式文件过程的
  4. 函数,这里的权限是私有的
  5. String text = entryText.getText().toString();//把输入的内容转化为字符串
  6. fos.write(text.getBytes());//把转化为字符串的内容转化为字节,然后写入
  7. //下面语句写在finally里面
  8. fos.flush();//把缓存里的内容写入到文件
  9. fos.close();//关闭流
  10. 读文件
  11. FileInputStream fis = null;//定义一个全局变量
  12. fis = openFileInput(FILE_NAME);//打开要读取的文件
  13. if (fis.available() == 0){//判断文件是否为空,为空就直接返回
  14. return;
  15. }
  16. byte[] readBytes = new byte[fis.available()];//把文件里的内容转化为字节
  17. while(fis.read(readBytes) != -1){//读文件,直到读到最后
  18. }
  19. String text = new String(readBytes);//把读到的字节转化为字符串
复制代码

读文件的第二种方法
  1. String path = "/data/data/cn.itcast.file/files/writeable.txt";//得到文件路径
  2. File file = new File(path);//创建一个文件对象
  3. FileInputStream inStream = new FileInputStream(file);//读文件
  4. byte[] buffer = new byte[1024];//缓存
  5. int len = 0;
  6. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  7. while( (len = inStream.read(buffer))!= -1){//直到读到文件结束
  8. outStream.write(buffer, 0, len);
  9. }
  10. byte[] data = outStream.toByteArray();//得到文件的二进制数据
  11. outStream.close();
  12. inStream.close();
  13. Log.i(TAG, new String(data));
复制代码

这里列举一个例子,主要是把写入的文件读出来显示在TextView中

第一步,修改布局文件main.xml
  1. View Code 
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView android:id="@+id/label"
  9. android:layout_width="fill_parent" 
  10. android:layout_height="wrap_content" 
  11. android:text="@string/hello"
  12. />
  13. <EditText android:id="@+id/entry"
  14. android:text="输入文件内容" 
  15. android:layout_width="fill_parent" 
  16. android:layout_height="wrap_content">
  17. </EditText>
  18. <LinearLayout android:id="@+id/LinearLayout01" 
  19. android:layout_width="wrap_content" 
  20. android:layout_height="wrap_content">
  21. <Button android:id="@+id/write"
  22. android:text="写入文件" 
  23. android:layout_width="wrap_content" 
  24. android:layout_height="wrap_content">
  25. </Button>
  26. <Button android:id="@+id/read"
  27. android:text="读取文件" 
  28. android:layout_width="wrap_content" 
  29. android:layout_height="wrap_content">
  30. </Button>
  31. </LinearLayout>
  32. <CheckBox android:id="@+id/append"
  33. android:text="追加模式" 
  34. android:layout_width="wrap_content" 
  35. android:layout_height="wrap_content">
  36. </CheckBox>
  37. <TextView android:id="@+id/display"
  38. android:text="文件内容显示区域" 
  39. android:layout_width="fill_parent" 
  40. android:layout_height="fill_parent"
  41. android:background="#FFFFFF" 
  42. android:textColor="#000000" >
  43. </TextView>
  44. </LinearLayout>
复制代码

第二步:写入核心代码,
  1. View Code 
  2. package cn.edu.zwu.tel;

  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.CheckBox;
  14. import android.widget.EditText;
  15. import android.widget.TextView;

  16. public class FileSaveTest01Activity extends Activity {

  17. private final String FILE_NAME = "Myfile01.txt";
  18. private TextView labelView;
  19. private TextView displayView;
  20. private CheckBox appendBox ;
  21. private EditText entryText;
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);

  26. labelView = (TextView)findViewById(R.id.label);
  27. displayView = (TextView)findViewById(R.id.display);
  28. appendBox = (CheckBox)findViewById(R.id.append);
  29. entryText = (EditText)findViewById(R.id.entry);
  30. Button writeButton = (Button)findViewById(R.id.write);
  31. Button readButton = (Button)findViewById(R.id.read);
  32. writeButton.setOnClickListener(writeButtonListener);
  33. readButton.setOnClickListener(readButtonListener);
  34. entryText.selectAll();
  35. entryText.findFocus();
  36. }


  37. OnClickListener writeButtonListener = new OnClickListener() { 
  38. @Override
  39. public void onClick(View v) {
  40. FileOutputStream fos = null; 
  41. try {
  42. if (appendBox.isChecked()){
  43. fos = openFileOutput(FILE_NAME,Context.MODE_APPEND);
  44. }
  45. else {
  46. fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
  47. }

  48. String text = entryText.getText().toString();
  49. fos.write(text.getBytes());
  50. labelView.setText("文件写入成功,写入长度:"+text.length());
  51. entryText.setText("");
  52. } catch (FileNotFoundException e) {
  53. e.printStackTrace();
  54. }
  55. catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. finally{
  59. if (fos != null){
  60. try {
  61. fos.flush();
  62. fos.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();

  65. }
  66. }

  67. };

  68. OnClickListener readButtonListener = new OnClickListener() { 
  69. @Override
  70. public void onClick(View v) {
  71. displayView.setText("");
  72. FileInputStream fis = null;
  73. try {
  74. fis = openFileInput(FILE_NAME);
  75. if (fis.available() == 0){
  76. return;
  77. }
  78. byte[] readBytes = new byte[fis.available()];
  79. while(fis.read(readBytes) != -1){
  80. }
  81. String text = new String(readBytes);
  82. displayView.setText(text);
  83. labelView.setText("文件读取成功,文件长度:"+text.length());
  84. } catch (FileNotFoundException e) {
  85. e.printStackTrace();
  86. }
  87. catch (IOException e) {
  88. e.printStackTrace();
  89. }


  90. };

  91. }
复制代码

效果图:
2012030918162182.png 

原文链接: http://www.cnblogs.com/shaoyangjiang/archive/2012/03/09/2388150.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值