今天,我們來記錄資料存放區的part2,討論以下關於File和SQLite的話題。我們知道,再關係型資料庫出現之前大家都習慣用檔案來儲存資料,後來出現了資料庫,能夠更加有效組織資料,我們首先來討論用檔案存取資料。
對於檔案,我們很容易想到對它的常用操作:建立、讀取和寫入等!Android也提供了這些方法,接下來看一下如何使用,很顯然,我們首先要獲得當前Activity的輸入資料流才能輸入資料,同理,我們也必須先獲得輸出資料流才能輸出資料,然而怎樣擷取當前Activity內容相關的輸入輸出資料流呢,這就用到了Context中的如下兩個方法:
下面我們來看一下兩個函數的定義,這樣就知道了參數的含義,以及傳回值等等資訊:
讀取和寫入有一個相同的參數,那就是輸入/輸出資料流的檔案名稱,輸出資料的方法還有第二個參數,也就是輸出的模式:有MODE_PRIVATE,這是預設模式,也可以是追加模式,如果檔案中已經存在內容,當再輸出內容到這個檔案之中的時候,新輸出的資料並不會覆蓋之前存在的資料,而是追加在已經存在的資料的後面,還有兩種模式是用來控制許可權的
接下來,我們就寫一個簡單的程式來使用一下這兩個函數,往一個檔案裡面寫入一些字串,然後在讀取,我們寫入一個不存在的檔案,這樣FileOutputStream就會建立這樣一個檔案。如果你是寫入一個人已經存在的檔案,而且寫入模式是追加模式,這樣有可能因為檔案中原本就存在資料造成了讀取的資料和剛寫入的資料不同。
第一步:建立一個新的工程File www.2cto.com
第二步:修改布局檔案,添加兩個按鈕,分別用於觸發寫入和讀取操作,再放置一個EditText用於編輯內容,放置一個TextView用於顯示讀取出來的內容
第三步:修改java源檔案,主要是為兩個按鈕添加事件監聽器,分別為讀寫操作,代碼如下:
public class FileActivity extends Activity {
private static final String NAME = "filetest.txt";
private Button write, read;
private EditText edit01;
private TextView view01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit01 = (EditText)findViewById(R.id.EditText01);
write = (Button)findViewById(R.id.write);
view01 = (TextView)findViewById(R.id.TextView01);
read = (Button)findViewById(R.id.read);
write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
FileOutputStream out = openFileOutput(NAME, MODE_APPEND);
out.write(edit01.getText().toString().getBytes());
}catch (Exception e) {
e.printStackTrace();
}
}
});
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
FileInputStream in = openFileInput(NAME);
byte[] buffer = new byte[in.available()];
in.read(buffer);
view01.setText(new String(buffer));
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
public class FileActivity extends Activity {
private static final String NAME = "filetest.txt";
private Button write, read;
private EditText edit01;
private TextView view01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit01 = (EditText)findViewById(R.id.EditText01);
write = (Button)findViewById(R.id.write);
view01 = (TextView)findViewById(R.id.TextView01);
read = (Button)findViewById(R.id.read);
write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
FileOutputStream out = openFileOutput(NAME, MODE_APPEND);
out.write(edit01.getText().toString().getBytes());
}catch (Exception e) {
e.printStackTrace();
}
}
});
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
FileInputStream in = openFileInput(NAME);
byte[] buffer = new byte[in.available()];
in.read(buffer);
view01.setText(new String(buffer));
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
第四步:運行程式,可看到如下效果:
摘自 chenlong12580的專欄