1 public class MainActivity extends ActionBarActivity { 2 3 private Button button; 4 private EditText edt; 5 private TextView textView; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 button = (Button)findViewById(R.id.button); 12 edt = (EditText)findViewById(R.id.editText); 13 textView = (TextView)findViewById(R.id.textView); 14 button.setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View v) { 17 WriteText(edt.getText().toString()); 18 textView.setText(ReadFile()); 19 } 20 }); 21 } 22 23 //保存数据 24 public void WriteText(String content){ 25 try { 26 FileOutputStream fos = openFileOutput("a.txt", MODE_PRIVATE); 27 fos.write(content.getBytes()); 28 fos.close(); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 } 33 34 //得到数据 35 public String ReadFile() { 36 String content = null; 37 try { 38 FileInputStream fis = openFileInput("a.txt"); 39 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 40 byte[] buffer = new byte[1024]; 41 int len = 0; 42 while((len = fis.read(buffer))!=-1){ 43 baos.write(buffer,0,len); 44 } 45 content = baos.toString(); 46 baos.close(); 47 fis.close(); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 return content; 52 } 53 }