01 |
//第一个button的事件读取
|
02 |
btn01.setOnClickListener(new Button.OnClickListener() {
|
03 |
|
04 |
@Override |
05 |
public void onClick(View v) { |
06 |
FileInputStream myFileStream = null;
|
07 |
InputStreamReader myReader = null;
|
08 |
|
09 |
char[]
inputBuffer = new char[255];
|
10 |
String data = null;
|
11 |
|
12 |
try{
|
13 |
//得到文件流对象
|
14 |
myFileStream = openFileInput("my.txt");
|
15 |
//得到读取器对象
|
16 |
myReader = new InputStreamReader(myFileStream);
|
17 |
//开始读取
|
18 |
myReader.read(inputBuffer);
|
19 |
data = new String(inputBuffer);
|
20 |
Toast.makeText(MyManagerFile.this, "读取文件成功",Toast.LENGTH_SHORT).show();
|
21 |
} |
22 |
catch (Exception e) { |
23 |
e.printStackTrace();
|
24 |
Toast.makeText(MyManagerFile.this, "读取文件失败",Toast.LENGTH_SHORT).show();
|
25 |
} |
26 |
finally {
|
27 |
try {
|
28 |
myReader.close();
|
29 |
myFileStream.close();
|
30 |
} catch (IOException e) {
|
31 |
e.printStackTrace();
|
32 |
} |
33 |
} |
34 |
|
35 |
//显示文件内容在txtView
|
36 |
txtView.setText("读取到的内容是:"+data);
|
37 |
|
38 |
|
39 |
|
40 |
} |
41 |
}); |
42 |
|
43 |
|
44 |
|
45 |
//第二个button的事件写入 |
46 |
btn02.setOnClickListener(new Button.OnClickListener() {
|
47 |
|
48 |
@Override |
49 |
public void onClick(View v) { |
50 |
|
51 |
//要写放的数据从文本框得到
|
52 |
String
data=((EditText)findViewById(R.id.EditText01)).getText().toString();
|
53 |
//文件流 |
54 |
FileOutputStream myFileStream = null;
|
55 |
//写对象 |
56 |
OutputStreamWriter myWriter = null;
|
57 |
|
58 |
try{
|
59 |
//从得到文件流对象
|
60 |
myFileStream = openFileOutput("my.txt",MODE_PRIVATE);
|
61 |
//得到写入器对象
|
62 |
myWriter = new OutputStreamWriter(myFileStream);
|
63 |
//开始写入
|
64 |
myWriter.write(data);
|
65 |
myWriter.flush();
|
66 |
Toast.makeText(MyManagerFile.this, "写入文件成功",Toast.LENGTH_SHORT).show();
|
67 |
} |
68 |
catch (Exception e) |
69 |
{
|
70 |
e.printStackTrace();
|
71 |
Toast.makeText(MyManagerFile.this, "写入文件失败",Toast.LENGTH_SHORT).show();
|
72 |
} |
73 |
finally |
74 |
{ |
75 |
try {
|
76 |
myWriter.close();
|
77 |
myFileStream.close();
|
78 |
} |
79 |
catch (IOException e) |
80 |
{ |
81 |
e.printStackTrace();
|
82 |
} |
83 |
} |
84 |
|
85 |
//显示文件内容在txtView
|
86 |
txtView.setText("刚刚写入的内容是:"+data);
|
87 |
|
88 |
|
89 |
} |
90 |
}); |
?文件保到那里去了,这里
浙公网安备 33010602011771号