Back to project page DataPersistence.
The source code is released under:
Apache License
If you think the Android project DataPersistence listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.datapersistencedemo; /*from w ww.ja va 2 s . co m*/ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; //???openFileOutput????????????????????openFileinput?????????????????? public class MainActivity extends Activity { private EditText inputText; private TextView outpuTextView; private Button saveButton,loadButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.file_io); init(); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (!TextUtils.isEmpty(inputText.getText())) { onSave(inputText.getText().toString()); } } }); loadButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { outpuTextView.setText(onLoad()); } }); } private String onLoad() { InputStream in = null; BufferedReader br = null; StringBuilder content = new StringBuilder(); try { in = openFileInput("EditTextdata"); br = new BufferedReader(new InputStreamReader(in)); String line = ""; while((line = br.readLine()) != null){ content.append(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if (br != null) { try { br.close(); Toast.makeText(MainActivity.this, "onLoad method is valid!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } } return content.toString(); } private void onSave(String inputText) { //??EditText-et_input??????????java??????EditTextdata????? OutputStream os = null; BufferedWriter bw = null; try { os = openFileOutput("EditTextdata", MODE_PRIVATE); bw = new BufferedWriter(new OutputStreamWriter(os)); bw.write(inputText); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if (bw != null) { Toast.makeText(MainActivity.this, "onSave method is valid.", Toast.LENGTH_SHORT).show(); bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } private void init() { inputText = (EditText) findViewById(R.id.et_input); outpuTextView = (TextView) findViewById(R.id.tv_output); saveButton = (Button) findViewById(R.id.btn_save); loadButton = (Button) findViewById(R.id.btn_load); } }