儲存基本型態的資料
如果應用程式使用的資料量不大
那麼可以考慮用SharedPreference
應用:
遊戲的紀錄
聲音開與關
等程式相關狀態
用SharedPreference創立的資料只限於該應用程式使用
當然可以在getSharedPreferences設定公開檔案給其他應用程式使用
SharedPreferences sp = getSharedPreferences("YYP.dat",MODE_PRIVATE);
以XML格式將資料存放到/data/data/com.example.fishingmapmaker/shared_prefs/之下
如果在實體手機操作那麼
/data必須是root權限才可以讀取
public class MainActivity extends Activity {
private Button click1, click2, click3;
private TextView tv, tv2, tv3;
private EditText iodata1, iodata3;
private SharedPreferences sp;
//宣告sharedpreference物件
private SharedPreferences.Editor editor;
//宣告sharedpreference編輯器
private String username;
private int stage;
private boolean sound;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = getSharedPreferences("YYPdata", MODE_PRIVATE);
//getSharedPreferences是context裡面的method
//建立sharedpreference儲存資料的檔案 並宣告存取權限為只限於本程式閱讀
editor= sp.edit();
//調出sharedpreference編輯器
click1 = (Button)findViewById(R.id.click1);
tv = (TextView)findViewById(R.id.tv);
click2 = (Button)findViewById(R.id.click2);
tv2 = (TextView)findViewById(R.id.tv2);
iodata1 = (EditText)findViewById(R.id.iodata1);
click3 = (Button)findViewById(R.id.click3);
tv3 = (TextView)findViewById(R.id.tv3);
iodata3 = (EditText)findViewById(R.id.iodata3);
click1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
savePreference();
}
});
readPreference();
click2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
saveInnerData();
}
});
readInnerData();
click3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
saveSDData();
}
});
readSDData();
}
private void savePreference(){
editor.putBoolean("sound", false);//每個資料型態都有對應的method
editor.putString("username", "Brad");//存進檔案
editor.putInt("stage", 118);
editor.commit();//務必要寫這行,才會將資料存進檔案
readPreference();
}
private void readPreference(){
username = sp.getString("username", "nobody");//讀取sharedpreference的儲存內容
stage=sp.getInt("stage", 0);
sound=sp.getBoolean("sound", true);
tv.setText("UserName: " + username + "\n" +
"Sound: " + (sound?"On":"Off") + "\n" +
"Stage: " + stage);
}
private void saveInnerData(){
String data = iodata1.getText().toString();//EditView的輸入文字記得要在末端加上toString(),因為getText()是View型態
try{
FileOutputStream fout
FileOutputStream fout = openFileOutput("YYP.dat",MODE_PRIVATE);
FileOutputStream fout= new FileOutputStream("YYP.dat");//錯誤寫法 filenotfound
//在純翠java 會預設純到專案跟目錄
fout.write(data.getBytes());//因為檔案皆為字串所以要將字串型別轉成byte型式
fout.flush();
fout.close();
Toast.makeText(this, "save OK", Toast.LENGTH_SHORT).show();
readInnerData();
}catch(IOException e){
Toast.makeText(this,"IO Exception", Toast.LENGTH_SHORT).show();
}
}
private void readInnerData(){
try{
// FileInputStream fin = new FileInputStream("YYP.dat");//出現fileNotFoundException
FileInputStream fin = openFileInput("YYP.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
String line;
while((line=reader.readLine())!=null){
tv.append(line+"\n");
}
fin.close();
}catch(IOException e){
Toast.makeText(this,"save ok",Toast.LENGTH_SHORT ).show();
}
}
//讀取寫入SDcard
private void saveSDData(){
String data = iodata3.getText().toString();
try{
FileWriter writer = new FileWriter("/mnt/sdcard/brad.dat");
writer.write(data);
writer.flush();
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
private void readSDData(){
try{
BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/brad.dat"));
String line;
while((line=reader.readLine())!=null){
tv3.append(line+"\n");
}
reader.close();
}catch(Exception e){
}
}
}
InnerStorage:
inner storage儲存在/data/data/com.example/files/之下
SDcardStorage
將資料儲存在SDcard
沒有留言:
張貼留言