接著上面一章,這次我們將數據存儲在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的專欄