1 |
package soo.data.file.io; |
---|
2 |
|
---|
3 |
import java.io.BufferedReader; |
---|
4 |
import java.io.IOException; |
---|
5 |
import java.io.InputStream; |
---|
6 |
import java.io.InputStreamReader; |
---|
7 |
import java.io.OutputStream; |
---|
8 |
import java.io.PrintWriter; |
---|
9 |
|
---|
10 |
import org.apache.http.message.BufferedHeader; |
---|
11 |
|
---|
12 |
import android.app.Activity; |
---|
13 |
import android.os.Bundle; |
---|
14 |
import android.util.Log; |
---|
15 |
import android.view.View; |
---|
16 |
import android.view.View.OnClickListener; |
---|
17 |
import android.widget.Button; |
---|
18 |
import android.widget.EditText; |
---|
19 |
|
---|
20 |
public class FileRWTest extends Activity { |
---|
21 |
private static final String F_NAME = "test2.txt"; |
---|
22 |
/** Called when the activity is first created. */ |
---|
23 |
EditText et; |
---|
24 |
@Override |
---|
25 |
public void onCreate(Bundle savedInstanceState) { |
---|
26 |
super.onCreate(savedInstanceState); |
---|
27 |
setContentView(R.layout.main); |
---|
28 |
|
---|
29 |
et = (EditText)findViewById(R.id.editText01); |
---|
30 |
final Button b1 = (Button)findViewById(R.id.button01); |
---|
31 |
final Button b2 = (Button)findViewById(R.id.button02); |
---|
32 |
final Button b3 = (Button)findViewById(R.id.button03); |
---|
33 |
|
---|
34 |
//����쎄린 |
---|
35 |
b1.setOnClickListener(new OnClickListener() { |
---|
36 |
|
---|
37 |
@Override |
---|
38 |
public void onClick(View v) { |
---|
39 |
// TODO Auto-generated method stub |
---|
40 |
fRead(); |
---|
41 |
} |
---|
42 |
}); |
---|
43 |
|
---|
44 |
//����곌린 |
---|
45 |
b2.setOnClickListener(new OnClickListener() { |
---|
46 |
|
---|
47 |
@Override |
---|
48 |
public void onClick(View v) { |
---|
49 |
// TODO Auto-generated method stub |
---|
50 |
fWrite(); |
---|
51 |
} |
---|
52 |
}); |
---|
53 |
|
---|
54 |
b3.setOnClickListener(new OnClickListener() { |
---|
55 |
|
---|
56 |
@Override |
---|
57 |
public void onClick(View v) { |
---|
58 |
// TODO Auto-generated method stub |
---|
59 |
finish(); |
---|
60 |
} |
---|
61 |
}); |
---|
62 |
} |
---|
63 |
@Override |
---|
64 |
public void onResume(){ |
---|
65 |
super.onResume(); |
---|
66 |
fRead(); |
---|
67 |
} |
---|
68 |
|
---|
69 |
@Override |
---|
70 |
public void onPause(){ |
---|
71 |
super.onPause(); |
---|
72 |
fWrite(); |
---|
73 |
} |
---|
74 |
|
---|
75 |
protected void fWrite() { |
---|
76 |
// TODO Auto-generated method stub |
---|
77 |
OutputStream os = null; |
---|
78 |
PrintWriter pw=null; |
---|
79 |
try{ |
---|
80 |
os = this.openFileOutput(F_NAME, this.MODE_PRIVATE); |
---|
81 |
pw = new PrintWriter(os,true); |
---|
82 |
pw.write(et.getText().toString()+"\n"); |
---|
83 |
}catch(IOException ie){ |
---|
84 |
Log.e("fWrite()",ie.toString()); |
---|
85 |
}finally{ |
---|
86 |
try{ |
---|
87 |
if(pw !=null) pw.close(); |
---|
88 |
if(os !=null) os.close(); |
---|
89 |
}catch(IOException ie2){} |
---|
90 |
} |
---|
91 |
} |
---|
92 |
|
---|
93 |
protected void fRead() { |
---|
94 |
// TODO Auto-generated method stub |
---|
95 |
InputStream is = null; |
---|
96 |
InputStreamReader isr = null; |
---|
97 |
BufferedReader br = null; |
---|
98 |
try{ |
---|
99 |
is = openFileInput(F_NAME); |
---|
100 |
if(is !=null){ |
---|
101 |
isr = new InputStreamReader(is); |
---|
102 |
br = new BufferedReader(isr); |
---|
103 |
String str = null; |
---|
104 |
StringBuffer sb = new StringBuffer(); |
---|
105 |
|
---|
106 |
while((str =br.readLine()) !=null){ |
---|
107 |
sb.append(str+"\n"); |
---|
108 |
} |
---|
109 |
et.setText(sb.toString()); |
---|
110 |
} |
---|
111 |
}catch(IOException ie){ |
---|
112 |
Log.e("fRead()",ie.toString()); |
---|
113 |
}finally{ |
---|
114 |
try{ |
---|
115 |
if(br !=null) br.close(); |
---|
116 |
if(isr !=null) isr.close(); |
---|
117 |
if(is !=null) is.close(); |
---|
118 |
}catch(IOException ie){} |
---|
119 |
} |
---|
120 |
} |
---|
121 |
} |
---|