Android File数据存储

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:andro>34 </LinearLayout>

 

 1 package com.turboradio.activity;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 import android.app.Activity;
9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.EditText;
12
13 public class FileSaveActivity extends Activity{
14 // 文件名称
15 private static final String FILE_NAME = "temp.txt";
16 private EditText writeEdit;
17 private EditText readEdit;
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.file_save);
22 writeEdit = (EditText)findViewById(R.id.write_edit);
23 readEdit = (EditText)findViewById(R.id.read_edit);
24 }
25 /**
26 * 写文件
27 */
28 public void writeFile(View v){
29 write(writeEdit.getText().toString());
30 }
31 /**
32 * 读文件
33 */
34 public void readFile(View v){
35 readEdit.setText(read());
36 }
37 private String read(){
38 try {
39 FileInputStream fis = openFileInput(FILE_NAME);
40 byte [] bytes = new byte [1024];
41 fis.read(bytes);
42 return new String(bytes);
43 } catch (FileNotFoundException e) {
44 e.printStackTrace();
45 } catch (IOException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49 return null;
50 }
51 private void write(String content){
52 try {
53 FileOutputStream fos = openFileOutput(FILE_NAME,MODE_APPEND);
54 fos.write(content.getBytes());
55 fos.close();
56 } catch (FileNotFoundException e) {
57 e.printStackTrace();
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61 }
62 }




相关文章: