http://www.cnblogs.com/shaoyangjiang/archive/2012/3/9.html
Android 文件管理方法
Android使用的是基于Linux的文件系統(tǒng),對于文件的訪問和管理是通過權(quán)限設(shè)置來限制的.
在Linux系統(tǒng)中,文件權(quán)限分別描述了創(chuàng)建者、同組用戶和其他用戶對文件的操作限制。
x表示可執(zhí)行,r表示可讀,w表示可寫,d表示目錄,-表示普通文件。
產(chǎn)生這樣的文件權(quán)限與程序人員設(shè)定的
Android 存儲文件的類型
(內(nèi)部存儲)程序開發(fā)人員可以建立和訪問程序自身的私有文件;
(資源存儲)可以訪問保存在資源目錄中的原始文件和XML文件;
(外部存儲)可以在SD卡等外部存儲設(shè)備中保存文件
Android系統(tǒng)允許應(yīng)用程序創(chuàng)建僅能夠自身訪問的私有文件,文件保存在設(shè)備的內(nèi)部存儲器上,在Linux系統(tǒng)下的/data/data/<package name>/files目錄中
Android系統(tǒng)不僅支持標準Java的IO類和方法,還提供了能夠簡化讀寫流式文件過程的函數(shù)
FileOutputStream openFileOutput(String filename int mode)
FileInputStream openFileInput(String filename)
參數(shù)文件不允許包含描述路徑的斜杠(其存儲位置固定)
訪問模式:
MODE_PRIVATE 私有模式,缺陷模式,文件僅能夠被文件創(chuàng)建程序訪問,或具有相同UID的程序訪問。
MODE_APPEND 追加模式,如果文件已經(jīng)存在,則在文件的結(jié)尾處添加新數(shù)據(jù)。
MODE_WORLD_READABLE 全局讀模式,允許任何程序讀取私有文件。
MODE_WORLD_WRITEABLE 全局寫模式,允許任何程序?qū)懭胨接形募?br />
三個基本的讀方法
abstract int read() :讀取一個字節(jié)數(shù)據(jù),并返回讀到的數(shù)據(jù),如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b) :將數(shù)據(jù)讀入一個字節(jié)數(shù)組,同時返回實際讀取的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b, int off, int len) :將數(shù)據(jù)讀入一個字節(jié)數(shù)組,同時返回實際讀取的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。off指定在數(shù)組b中存放數(shù)據(jù)的起始偏移位置;len指定讀取的最大字節(jié)數(shù)。
其它方法
long skip(long n) :在輸入流中跳過n個字節(jié),并返回實際跳過的字節(jié)數(shù)。
int available() :返回在不發(fā)生阻塞的情況下,可讀取的字節(jié)數(shù)。
void close() :關(guān)閉輸入流,釋放和這個流相關(guān)的系統(tǒng)資源。
void mark(int readlimit) :在輸入流的當(dāng)前位置放置一個標記,如果讀取的字節(jié)數(shù)多于readlimit設(shè)置的值,則流忽略這個標記。
void reset() :返回到上一個標記。
boolean markSupported() :測試當(dāng)前流是否支持mark和reset方法。如果支持,返回true,否則返回false。
三個基本的寫方法
abstract void write(int b) :往輸出流中寫入一個字節(jié)。
void write(byte[] b) :往輸出流中寫入數(shù)組b中的所有字節(jié)。
void write(byte[] b, int off, int len) :往輸出流中寫入數(shù)組b中從偏移量off開始的len個字節(jié)的數(shù)據(jù)。
其它方法
void flush() :刷新輸出流,強制緩沖區(qū)中的輸出字節(jié)被寫出。
void close() :關(guān)閉輸出流,釋放和這個流相關(guān)的系統(tǒng)資源。
對文件和流的操作容易引發(fā)異常,所以必須要用try-catch語句
主要核心代碼
首先是新建一個文件
private final String FILE_NAME = "Myfile01.txt";
寫文件
FileOutputStream fos = null;//聲明一個全局變量
//注意下面的語句要進行拋異常處理FileNotFoundException e,IOException e
fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);//寫流式文件過程的
函數(shù),這里的權(quán)限是私有的
String text = entryText.getText().toString();//把輸入的內(nèi)容轉(zhuǎn)化為字符串
fos.write(text.getBytes());//把轉(zhuǎn)化為字符串的內(nèi)容轉(zhuǎn)化為字節(jié),然后寫入
//下面語句寫在finally里面
fos.flush();//把緩存里的內(nèi)容寫入到文件
fos.close();//關(guān)閉流
讀文件
FileInputStream fis = null;//定義一個全局變量
fis = openFileInput(FILE_NAME);//打開要讀取的文件
if (fis.available() == 0){//判斷文件是否為空,為空就直接返回
return;
}
byte[] readBytes = new byte[fis.available()];//把文件里的內(nèi)容轉(zhuǎn)化為字節(jié)
while(fis.read(readBytes) != -1){//讀文件,直到讀到最后
}
String text = new String(readBytes);//把讀到的字節(jié)轉(zhuǎn)化為字符串
讀文件的第二種方法
String path = "/data/data/cn.itcast.file/files/writeable.txt";//得到文件路徑
File file = new File(path);//創(chuàng)建一個文件對象
FileInputStream inStream = new FileInputStream(file);//讀文件
byte[] buffer = new byte[1024];//緩存
int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1){//直到讀到文件結(jié)束
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//得到文件的二進制數(shù)據(jù)
outStream.close();
inStream.close();
Log.i(TAG, new String(data));
這里列舉一個例子,主要是把寫入的文件讀出來顯示在TextView中
第一步,修改布局文件main.xml
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView android:id="@+id/label"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 />
12 <EditText android:id="@+id/entry"
13 android:text="輸入文件內(nèi)容"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content">
16 </EditText>
17 <LinearLayout android:id="@+id/LinearLayout01"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content">
20 <Button android:id="@+id/write"
21 android:text="寫入文件"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content">
24 </Button>
25 <Button android:id="@+id/read"
26 android:text="讀取文件"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content">
29 </Button>
30 </LinearLayout>
31 <CheckBox android:id="@+id/append"
32 android:text="追加模式"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content">
35 </CheckBox>
36 <TextView android:id="@+id/display"
37 android:text="文件內(nèi)容顯示區(qū)域"
38 android:layout_width="fill_parent"
39 android:layout_height="fill_parent"
40 android:background="#FFFFFF"
41 android:textColor="#000000" >
42 </TextView>
43 </LinearLayout>
復(fù)制代碼
第二步:寫入核心代碼,
View Code
1 package cn.edu.zwu.tel;
2
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
17 public class FileSaveTest01Activity extends Activity {
18
19 private final String FILE_NAME = "Myfile01.txt";
20 private TextView labelView;
21 private TextView displayView;
22 private CheckBox appendBox ;
23 private EditText entryText;
24 @Override
25 public void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28
29 labelView = (TextView)findViewById(R.id.label);
30 displayView = (TextView)findViewById(R.id.display);
31 appendBox = (CheckBox)findViewById(R.id.append);
32 entryText = (EditText)findViewById(R.id.entry);
33 Button writeButton = (Button)findViewById(R.id.write);
34 Button readButton = (Button)findViewById(R.id.read);
35 writeButton.setOnClickListener(writeButtonListener);
36 readButton.setOnClickListener(readButtonListener);
37 entryText.selectAll();
38 entryText.findFocus();
39 }
40
41
42 OnClickListener writeButtonListener = new OnClickListener() {
43 @Override
44 public void onClick(View v) {
45 FileOutputStream fos = null;
46 try {
47 if (appendBox.isChecked()){
48 fos = openFileOutput(FILE_NAME,Context.MODE_APPEND);
49 }
50 else {
51 fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
52 }
53
54 String text = entryText.getText().toString();
55 fos.write(text.getBytes());
56 labelView.setText("文件寫入成功,寫入長度:"+text.length());
57 entryText.setText("");
58 } catch (FileNotFoundException e) {
59 e.printStackTrace();
60 }
61 catch (IOException e) {
62 e.printStackTrace();
63 }
64 finally{
65 if (fos != null){
66 try {
67 fos.flush();
68 fos.close();
69 } catch (IOException e) {
70 e.printStackTrace();
71 }
72 }
73 }
74 }
75 };
76
77 OnClickListener readButtonListener = new OnClickListener() {
78 @Override
79 public void onClick(View v) {
80 displayView.setText("");
81 FileInputStream fis = null;
82 try {
83 fis = openFileInput(FILE_NAME);
84 if (fis.available() == 0){
85 return;
86 }
87 byte[] readBytes = new byte[fis.available()];
88 while(fis.read(readBytes) != -1){
89 }
90 String text = new String(readBytes);
91 displayView.setText(text);
92 labelView.setText("文件讀取成功,文件長度:"+text.length());
93 } catch (FileNotFoundException e) {
94 e.printStackTrace();
95 }
96 catch (IOException e) {
97 e.printStackTrace();
98 }
99
100 }
101 };
102
103 }
復(fù)制代碼
效果圖: