接著上面一章,這次我們將資料存放區在File檔案裡,布局檔案沒什麼改變,還是一樣的布局,看main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:text="TextView"></TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:inputType="number">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout2" android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/addOne"
android:text="悟空又打死了一個妖怪"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/read_edit"
android:text="讀取Edit中的資料"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout1" android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/save"
android:text="儲存資料"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/read"
android:text="讀取資料"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/clear"
android:text="清除資料"></Button>
</LinearLayout>
</LinearLayout>
跟上一章的一樣,也是一個EditText和幾個按鈕
主java檔案:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Shared_PreferencesDemo extends Activity implements OnClickListener
{
private final String FILE_NAME = "demo.bin";
private int count = 0;
private String str;
private TextView text;
private EditText edit_text;
private Button add;
private Button save;
private Button read;
private View clear;
private Button read_edit;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.shared_preferences);
str = "悟空殺死了" + count + "只妖怪.";
text = (TextView)findViewById(R.id.textView1);
text.setText(str);
edit_text = (EditText)findViewById(R.id.editText1);
edit_text.setText(String.valueOf(count));
add = (Button)findViewById(R.id.addOne);
add.setOnClickListener(this);
read_edit = (Button)findViewById(R.id.read_edit);
read_edit.setOnClickListener(this);
save = (Button)findViewById(R.id.save);
save.setOnClickListener(this);
read = (Button)findViewById(R.id.read);
read.setOnClickListener(this);
clear = (Button)findViewById(R.id.clear);
clear.setOnClickListener(this);
}
//按鈕事件監聽
@Override
public void onClick(View v)
{
switch(v.getId()){
//悟空又打死了一個妖怪按鈕事件
case R.id.addOne:
//妖怪數量加1
count++;
//重新整理TextView和EditText控制項中的值
refresh();
break;
//讀取Edit中的資料按鈕事件
case R.id.read_edit:
//取出存在SharedPreferences中的值
count =Integer.parseInt(edit_text.getText().toString());
refresh();
break;
case R.id.save:
//自訂寫入資料方法
write(String.valueOf(count));
break;
case R.id.read:
//將讀取出的資料給count賦值
count = Integer.parseInt(read().trim());
refresh();
break;
case R.id.clear:
count = 0;
write(String.valueOf(count));
refresh();
break;
}
}
/**
* 從檔案中讀取資料,這是很簡單的java應用,IO操作
* @return
*/
private String read()
{
try
{
//開啟檔案輸入資料流
FileInputStream fis = openFileInput(FILE_NAME);
byte[] buffer = new byte[1024];
int hasRead = 0;
StringBuffer sb = new StringBuffer();
while((hasRead = fis.read(buffer)) > 0){
sb.append(new String(buffer, 0, hasRead));
}
return sb.toString();
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* 將資料寫入檔案,也是很簡單的IO操作
* @param string
*/
private void write(String string)
{
try
{
//以覆蓋方式開啟檔案輸出資料流
FileOutputStream fos = openFileOutput(FILE_NAME, 0);
//將FileOutputStream封裝成PrintStream
PrintStream ps = new PrintStream(fos);
//輸出檔案內容
ps.println(string);
ps.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
//自訂重新整理
private void refresh()
{
str = "悟空殺死了" + count + "只妖怪.";
text.setText(str);
edit_text.setText(String.valueOf(count));
}
}
這裡面主要是檔案的IO操作,也比較簡單,學過JAVA的人應該知道IO操作的,具體就看代碼吧,這裡就不多說了,:
這裡所不同的就是儲存路徑不一樣,同樣是存在data/data/你自己所在包檔案裡,但是建立在File檔案夾下面的,看圖:
OK,這一章也結束了,其實Android儲存方式也是比較簡單的,對於一些簡單的資料,可以採用這兩種儲存方式,如果比較大型或是複雜的資料就應該採用SQLite資料庫來儲存了,下一章我們會介紹SQLite資料庫的操作,謝謝
摘自:kangkangz4的專欄